laravel migration foreign key nullable

Viewed 256

I'm building a project with Laravel version 7.25.0. I coded my migration files and they contain some foreign keys. I added nullable() at the end of each foreign key but they don't work. I looked at similar questions but none of them solved my problem. Here is my migration file's up function

    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->tinyInteger('level')->nullable();
            $table->tinyInteger('try_count')->nullable();
            $table->char('room_name')->nullable();
            $table->boolean('timeless')->nullable();
            $table->integer('time')->nullable();
            $table->foreignId('school_id')->constrained('schools')->nullable();
            $table->foreignId('year_id')->constrained('years')->nullable();
            $table->foreignId('course_id')->constrained('courses')->nullable();
            $table->string('type')->nullable();
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            $table->text('description')->nullable();
            $table->boolean('timeout')->nullable();
            $table->boolean('question_sorting')->nullable();
            $table->timestamps();
        });
    }

when I migrate it with the other files and not getting any error, this is my phpmyadmin page of that table

phpadmin page table ss

1 Answers

Any additional column modifiers must be called before constrained,

So You must put the nullable() before constrained() like this :

$table->foreignId('course_id')->nullable()->constrained('courses');
Related