Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

foreign key in laravel 9

Firstly you have to make your user_id field an index:

$table->index('user_id');
After that you can create a foreign key with an action on cascade:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.

On down() function you have to do this and then on up() do what I've wrote above:

$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #foreign #key #laravel
ADD COMMENT
Topic
Name
4+8 =