

Given this setup, when a user record is deleted, all related posts and profile records will be deleted. Additionally, the Profile model has been set up to delete its related address records. Since the user is considered a parent of posts and profiles, these relationships have been added to the $cascadeDeletes property. In the example above, the CascadesDeletes trait has been added to the User model to enable cascading deletes. Return $ this-> morphsMany( Address::class, 'addressable') The Exception that caused the child not to be deleted will bubble up to where the delete() originally started, and will need to be caught and handled.** Code Example If, for any reason, a child record could not be deleted, the transaction will rollback and no records will be deleted at all. This makes the delete an "all or nothing" event. are deleted.Īdditionally, all cascading deletes are performed within a transaction. This will continue on until all children, grandchildren, great grandchildren, etc. Furthermore, in the case where a child record also has cascading deletes defined, the delete will cascade down and delete the related records of the child, as well. Now, when a parent record is deleted, the defined child records will also be deleted.

The $cascadeDeletes property should be set to an array of the relationships that should be deleted when a parent record is deleted. Once that is done, define the $cascadeDeletes property on the Model.
#LARAVEL ELOQUENT DELETE CASCADE UPDATE#
update your Model to use the \ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes trait.update your Model to extend the \ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletesModel, or.Composer require shiftonelabs/laravel-cascade-deletes UsageĮnabling cascading deletes can be done two ways.
