首页 > 解决方案 > Laravel 中的数据模型错误 (SQLSTATE[42S01])

问题描述

早上好,我正在尝试为车间工人应用程序做数据模型。问题来自指南和图像数据模型。每个向导都有自己的形象,每个形象都属于一个向导。当我尝试运行迁移并且 Laravel 抛出 SQLSTATE 并且我没有找到错误时,非常感谢您的帮助。

指南迁移:

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();
        });

导轨型号:

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;
    }


}

图片迁移:

        Schema::create('images', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('image');
            $table->timestamps();
        });

图像模型:

class Image extends Model
{
    protected $fillable = ['image'];

    public function images()
    {
        return $this -> belongsTo(Guide::class);
    }
}

更新:php 工匠:状态图像 php artisan:status 截图

标签: phplaraveldatabase-migration

解决方案


你可以尝试的事情:

  • 确保图像迁移文件位于指南迁移文件之前,因为 image_id 是指南表上的外键
  • php artisan migrate:reset

推荐阅读