What does onDelete('cascade') mean?

Short answer is: in your case, if you deleted a user, all posts related to him will be deleted too.

onDelete('cascade'); simply adds ON DELETE CASCADE rule to your database which specifies that the child data gets deleted when the parent data is deleted.

Note: take care of the typo (double semicolon)

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


The code you are talking about creates a foreign key / reference to a column on another table. The onDelete('cascade') means that when the row is deleted, it will delete all it's references and attached data too.

For example if you have a User which has a Post and you set onDelete('cascade') on the user, then when the user deletes his account, all posts will be deleted as well.

You can find more information on Laravel excellent documentation.


If you're using mysql, take a look at the documentation.

In your case it means: If the user is deleted, the post will also be deleted.

Tags:

Php

Laravel