Add "ON DELETE CASCADE" to existing column in Laravel

Farid Movsumov picture Farid Movsumov · Nov 8, 2014 · Viewed 45.3k times · Source

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?

Answer

Farid Movsumov picture Farid Movsumov · Nov 9, 2014

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');