Laravel Add Foreign Key to Existing Table Example

Throughout this laravel add foreign key to existing table tutorial you will learn how to add foreign key in existing table using laravel migration. We have added a simple and easy code to update the foreign key updating the relation parents and child table.

You can easily add foreign key in laravel exiting table using migration with laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 application.

Create New Migration

First you need to generate a new migartion using the following commad:

php artisan make:migration add_user_id_foreign_to_posts_table --table=posts

Now open the generated migration and add the code just like below:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class addUserIdForeignToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
             $table->integer('user_id');

             $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
      Schema::table('posts', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropColumn('user_id');
      });
    }
}

Now run the migration command to add new column in your database table;

php artisan migrate

If you need to add only foreign the column which is already added in table then add below code.

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
             $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
      Schema::table('posts', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
      });
    }

Change Foreing with Data in Laravel

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
             $table->integer('user_id')->unsigned()->change();
             $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
      Schema::table('posts', function (Blueprint $table) {
          $table->dropForeign(['user_id']);
      });
    }

See Here: Add Foreign Key Existing Table with Data

Thats all the channge or add foreign key in existing table laravel example is over now.. if you have still any issue please comment or mail us.

1 thought on “Laravel Add Foreign Key to Existing Table Example”

Leave a Comment