[英]Error with data model in Laravel (SQLSTATE[42S01])
Good morning, I'm trying to do the data models for a workshop workers app.早上好,我正在尝试为车间工人应用程序创建数据模型。 The problem comes with the guides and images data models.问题来自指南和图像数据模型。 Each guide has his own image, and every image belongs to a guide.每个向导都有自己的形象,每个形象都属于一个向导。 When I try to run the migrations and Laravel throws the SQLSTATE and I don't find the error, so I would appreciate your help.当我尝试运行迁移并且 Laravel 抛出 SQLSTATE 并且我没有发现错误时,所以我很感激你的帮助。
The guides migration:指南迁移:
Schema::create('guides', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('topic')->nullable();
$table->string('author')->nullable();
$table->year('year')->nullable();
$table->text('goal')->nullable();
$table->text('description')->nullable();
$table->smallInteger('assistants_quantity')->nullable();
$table->smallInteger('duration_hours')->nullable();
$table->string('methodology')->nullable();
$table->bigInteger('image_id')->unsigned();
$table->foreign('image_id')->references('id')->on('images');
$table->timestamps();
$table->softDeletes();
});
The guides model:向导模型:
class Guide extends Model
{
protected $table = 'guides';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = ['title', 'topic', 'author', 'year', 'goal', 'description', 'assistants_quantity', 'duration_hours', 'methodology'];
protected $visible = ['title','topic', 'author', 'year', 'goal', 'description', 'assistants_quantity', 'duration_hours', 'methodology'];
public function materials()
{
return $this -> belongsToMany(Material::class);
}
public function images()
{
return $this -> belongsTo(Image::class);
}
static function search($keyword)
{
$result = Guide::where('title', 'LIKE', '%' . $keyword . '%')->get();
return $result;
}
}
The Images migration:图片迁移:
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('image');
$table->timestamps();
});
The Images model:图像模型:
class Image extends Model
{
protected $fillable = ['image'];
public function images()
{
return $this -> belongsTo(Guide::class);
}
}
Things you can try:你可以尝试的事情:
php artisan migrate:reset
运行php artisan migrate:reset
Move your image migration file upper to your guides migration file since in your guide migration file you are referencing the images_id in your foreign key.将您的图像迁移文件移至您的指南迁移文件上方,因为在您的指南迁移文件中,您正在引用外键中的 images_id。 To move it.要移动它。 just update the date of the images migration file.只需更新图像迁移文件的日期。
Since your guide migration was created on
2019_12_10
, your images table should be2019_12_09 or lower
.由于您的指南迁移是在2019_12_10
创建的,2019_12_10
您的图像表应为2019_12_09 or lower
。 Just rename it.只需重命名即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.