Laravel 中的数据模型错误 (SQLSTATE[42S01])

Error with data model in 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 artisan:status 图片

顺序很重要,因为您的 'guides' 迁移取决于 'images' 主键:'images' 迁移必须在 'guides' 之前执行。

您必须更改迁移文件的顺序。 看到这个

您可以尝试的事情:

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

将您的图像迁移文件移到指南迁移文件的上方,因为在您的指南迁移文件中您引用了外键中的 images_id。移动它。只需更新图像迁移文件的日期。

Since your guide migration was created on 2019_12_10, your images table should be 2019_12_09 or lower. Just rename it.