How to add foreign key column – Laravel 5.8

Let’s go through this tutorial on how to add foreign key in Laravel migration.

$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Adding a foreign key column in Laravel 5.8.

update your `integer('user_id')` to `bigInteger('user_id')`
public function up() { 
        Schema::create('evaluation', function (Blueprint $table) { 
            $table->increments('id'); 
            $table->bigInteger('user_id')->unsigned()->index(); 
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

Create a foreign key in Laravel migration as shown below.

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');

    $table->foreign('user_id')->references('id')->on('users');
});

Migrate the foreign key in Laravel.

$table->foreign('column_name')->references('id')->on('table_name')->onDelete('cascade');