Available actions for onUpdate / onDelete in Laravel 5.x

bobD picture bobD · Sep 15, 2016 · Viewed 20.8k times · Source

As mentioned in here, we can use the word cascade when making a relation in migrations
but I wonder they didn't say anything about other actions when deleting or updating a foreign key

so I'm not sure if there is such thing or not:

$table->foreign('user_id')
  ->references('id')->on('users')
  ->onDelete('set null');
  //->onDelete('set_null');
  //->onDelete('setNull');

or the same thing about onUpdate and about no action just like the phpMyAdmin

enter image description here


thanks

Answer

Rafael Berro picture Rafael Berro · Sep 15, 2016

You can do all the options mentioned in phpmyadmin this way:

$table->...->onDelete('CASCADE');
$table->...->onDelete('SET NULL');
$table->...->onDelete('RESTRICT');

// do not call the onDelete() method if you want the RESTRICT option.

You have to make sure you set the foreign key field as nullable:

$table->...->unsigned()->nullable();