Laravel - add foreign key on existing table with data

The holding_id column should be unsigned

Create a new migration file and migrate it, migration code should be like this :

Schema::table('objects', function (Blueprint $table) {
    $table->integer('holding_id')->unsigned()->change();

    $table->foreign('holding_id')->references('id')->on('holdings');
});

The change() method is called to change the structure of existing column.

It's not necessary to call onDelete("NO ACTION") method.


Thanks Mohammad but this solution didn't work for me as I am Laravel 5.4 and have different case here that my other table is already exists, Here what I found may it help some one.

Schema::table('objects', function (Blueprint $table) {
    $table->integer('holding_id')->unsigned()->index()->nullable();

    $table->foreign('holding_id')->references('id')->on('holdings');
});

with index() and nullable() it made the trick.

Edit No need for index() it just need to be nullable()