Page 1 of 1

Understanding onDelete cascade in laravel migrations

Posted: Sat Feb 11, 2017 1:49 pm
by gautamz07
I was going through this section in the laravel documentation:
https://laravel.com/docs/5.4/migrations ... onstraints

I saw the below line of code:

Code: Select all

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');
I am not quite understanding the onDelete part , can somebody please explain that to me ?

Thank you.
Gautam.

Re: Understanding onDelete cascade in laravel migrations

Posted: Sun Feb 12, 2017 8:24 am
by Celauran
You have a user model. Comments belong to a user. With cascading deletes, deleting a user would also delete all their comments.

Re: Understanding onDelete cascade in laravel migrations

Posted: Sun Feb 12, 2017 11:11 am
by gautamz07
Thanks got it ! :)