Understanding onDelete cascade in laravel migrations

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Understanding onDelete cascade in laravel migrations

Post 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.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Understanding onDelete cascade in laravel migrations

Post by Celauran »

You have a user model. Comments belong to a user. With cascading deletes, deleting a user would also delete all their comments.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Understanding onDelete cascade in laravel migrations

Post by gautamz07 »

Thanks got it ! :)
Post Reply