Available Actions For OnUpdate / OnDelete In Laravel 5.x


Answer :

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

Referring to the source code:

`vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/Grammar.php` in the function compileForeign() 

It just appends whatever you pass in to the table query.

    if (! is_null($command->onDelete)) {         $sql .= " on delete {$command->onDelete}";     }      if (! is_null($command->onUpdate)) {         $sql .= " on update {$command->onUpdate}";     } 

So, make sure you pass one of the following: "cascade", "no action", "restrict", or "set null"

NOTE: Do NOT use underscores in the actions like "set_null" and "no_action"


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?