JellyBool 大神,继续追问一下问题
数据表结构设计:
关系是:(一篇“文章”对应一个“文章分类”、一种“文章分类” 对应多篇“文章”)
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('subject')->index()->comment('主题');
$table->integer('category_no')->unique()->index(); //categories表中外键
}
public function up()
{
Schema::create('article_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique()->index();
$table->integer('sort')->default(1)->unique()->index();
$table->timestamps();
});
}
//以上我那样设计数据表行吗?
//Model如下:
class Article extends Model
{
public function category()
{
return $this->belongsTo(ArticleCategory::class, 'article_id');
// 这样设置关联关系对吗?
}
}
class ArticleCategory extends Model
{
public function articles()
{
return $this->belongsTo(Article::class, 'article_id');
//这块这样写对吗?
}
}