【发布时间】:2020-06-12 16:10:56
【问题描述】:
所以我的 laravel 项目中出现了这个错误。我在我的数据库中创建了blogs 和categories 列。我的迁移:
分类:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->timestamps();
});
博客:
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 100);
$table->string('description', 900);
$table->string('image');
$table->bigInteger('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
这些是模型之间的关系:
我的Blog 模特:
public function category(){
return $this->hasOne('App\Models\Category');
}
还有我的Category 模特:
public function blogs(){
return $this->belongsTo('App\Models\Blog', 'category_id');
}
当我创建一个新的博客文章时,它会显示这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.blog_id' in 'where clause' (SQL: select * from `categories` where `categories`.`blog_id` = 16 and `categories`.`blog_id` is not null limit 1)
但我的博客文章已正确存储在category_id 的数据库中。我做错了什么?
【问题讨论】: