Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackFlick's avatar

Best Practice for Laravel Migration with Relationships

I came across a scenario where I have a 2 tables and 1 for my forms and other is a relationship table.

Let's say I have a sections table and the sections has students

here's the migration

public function up()
    {
        Schema::create('section_has_students', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreignId('section_id')->constrained()->onDelete('cascade');
            $table->foreignId('student_id')->constrained()->onDelete('cascade');
            $table->timestamps();
            Schema::create('sections', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->foreignId('course_id')->constrained()->onDelete('cascade');
                $table->string('name');
                $table->time('time_start');
                $table->time('time_end');
                $table->string('adviser');
                $table->timestamps();
            });
        });
        
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sections');
        Schema::dropIfExists('section_has_students');
    }

Now for example it is already deployed in production and I need to remove the section_has_students and move it to the sections table like this

public function up()
    {
            Schema::create('sections', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->foreignId('course_id')->constrained()->onDelete('cascade');
		$table->foreignId('student_id')->constrained()->onDelete('cascade');
                $table->string('name');
                $table->time('time_start');
                $table->time('time_end');
                $table->string('adviser');
                $table->timestamps();
            });
    }

What's the best practice for this scenario if there is a relationship involved.

0 likes
1 reply
thewebartisan7's avatar
Level 14

You can create a one migration for all this tables, and then drop them in right sequence.

Otherwise you can use:


Schema::disableForeignKeyConstraints();

// Drop table and don't worry about foreign key constraints

Schema::enableForeignKeyConstraints();


Please or to participate in this conversation.