Sometimes you may face this error like laravel migration foreign key constraint is incorrectly formed. In this tutorial, I will show you laravel foreign key constraint in migration. From this tutorial, you will learn how to define foreign key in laravel migration in laravel 8 and laravel 9 application.

I will show you many ways to define laravel foreign key constraint in migration. I will show you also laravel on delete cascade example and laravel on update cascade example. So let's see the example code of laravel foreign key constraint in migration.

 

Laravel 9 foreign key constraint : Example one

In this example, I will use foreignIdFor() method to define laravel foreign key constraint in laravel migration. Let's see the example:

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(User::class)
                ->constrained()
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
            $table->timestamps();
        });
    }

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

 

Laravel 9 foreign key constraint : Example two

In this example, I will use foreignId() method to define laravel foreign key constraint in laravel migration. Let's see the example:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
                ->constrained()
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
            $table->timestamps();
        });
    }

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

 

Read also: Create Model Controller Factory Migration In Laravel With One Command

 

Laravel 9 foreign key constraint : Example three

Now I am going to use the third example, Here I will use the normal Laravel example, which is used to force referential integrity at the database level. For example, let's define a user_id column on the posts table that references the id column on a users table:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')
                 ->references('id')
                 ->on('users')
                 ->onUpdate('cascade')
                 ->onDelete('cascade');
});

 

Now if you run migration, then you will see the below output in your mysql table table:

laravel-9-foreign-key-constraint

Conclusion

I have tried to discuss the clear concept of laravel 9 foreign key constraint. Now we know laravel migration foreign key example. Hope this laravel foreign key constraint in migration tutorial will help you.