在DB中创建模型和两个新表的关系
答
你应该建立两个模型1)标签2)后,如:
1)标签
<?php
namespace App\Models\frontend;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'tag';
/**
* The database primary key value.
*
* @var string
*/
protected $guarded = ['id', '_token'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['name'];
/**
* That belong to the Tag.
*/
public function post()
{
return $this->hasMany('App\Models\Post');
}
}
2)邮政
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'post';
/**
* The database primary key value.
*
* @var string
*/
protected $guarded = ['id', '_token'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['text','id_tag'];
/**
* The roles that belong to the Post.
*/
public function tag()
{
return $this->belongsTo('App\Models\Tag','id_tag');
}
}
希望这对你的工作!
对于Model'php artisan make:model ModelName'在命令中使用 AND转到https://laravel.com/docs/5.4/queries#joins –