Add "ON DELETE CASCADE" to existing column in Laravel

PhpLaravelLaravel 4Database Migration

Php Problem Overview


I have user_id fk column in my table

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

I should add on cascade delete feature to this existing column. How can I do this?

Php Solutions


Solution 1 - Php

Drop foreign key first. Thanks to Razor for this tip

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

Solution 2 - Php

In my case, i'll need to put the col name in an array else that will be an error.

Schema::table('transactions', function (Blueprint $table) {
    $table->dropForeign(['transactions_order_id_foreign']);
    $table->foreign('order_id')
        ->references('id')->on('orders')
        ->onDelete('cascade')
        ->change();
});

mysql 5.7 ver

Solution 3 - Php

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

Solution 4 - Php

Laravel schema builder can't modify columns at the current state, so you will use raw queries. You will have to drop and recreate the constraint:

PostgreSQL

function up()
{
    DB::statement('alter table answers drop constraint answers_user_id_foreign,
                   add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id)
                   on delete cascade;'
    );
}
function down()
{
    DB::statement('alter table answers drop constraint answers_user_id_foreign,
                   add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id);'
    );
}

MySQL

function up()
{
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;');
    DB::statement('alter table answers add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id)
                   on delete cascade;'
    );
}
function down()
{
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;');
    DB::statement('alter table answers add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id);'
    );
}

Solution 5 - Php

Thanks for question answer. Help me get to this working code in L5.1 :

public function up()
{
    Schema::table('transactions', function (Blueprint $table) {
        $table->dropForeign('transactions_order_id_foreign');
        $table->foreign('order_id')
            ->references('id')->on('orders')
            ->onDelete('cascade')
            ->change();
    });

    Schema::table('orders', function (Blueprint $table) {
        $table->dropForeign('orders_user_id_foreign');
        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade')
            ->change();
    });
}

Solution 6 - Php

Use the unsigned function to user_id in the present migration:

$table->interger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('table_name')->onDelete('cascade');

Solution 7 - Php

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

I am assuming you used Illuminate\Database\Schema\Blueprint::primary() to create users.id. If that is the case, then users.id will be unsigned. Therefore your foreign key column user_id must also be unsigned.

Solution 8 - Php

Small code

$table->foreignId('user_id')->constrained('users');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFarid MovsumovView Question on Stackoverflow
Solution 1 - PhpFarid MovsumovView Answer on Stackoverflow
Solution 2 - PhpshalonteohView Answer on Stackoverflow
Solution 3 - PhpMark BakerView Answer on Stackoverflow
Solution 4 - PhpRazorView Answer on Stackoverflow
Solution 5 - PhpJohnWolfView Answer on Stackoverflow
Solution 6 - PhpDetroit CharanView Answer on Stackoverflow
Solution 7 - PhpInda5thView Answer on Stackoverflow
Solution 8 - PhpJesvinView Answer on Stackoverflow