Ad

Error With Data Model In Laravel (SQLSTATE[42S01])

- 1 answer

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.

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

Update: php artisan:status image Screenshot of php artisan:status

Ad

Answer

Order matter, since your 'guides' migration depends on 'images' primary key : 'images' migration must be executed before 'guides'.

You must change your migration files order. see this link

Ad
source: stackoverflow.com
Ad