PHPackages                             shiftonelabs/laravel-cascade-deletes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. shiftonelabs/laravel-cascade-deletes

ActiveLibrary[Database &amp; ORM](/categories/database)

shiftonelabs/laravel-cascade-deletes
====================================

Adds application level cascading deletes to Eloquent Models.

2.0.2(1y ago)165735.6k↓22.5%102MITPHPPHP &gt;=8.0.2CI failing

Since Dec 7Pushed 1y ago4 watchersCompare

[ Source](https://github.com/shiftonelabs/laravel-cascade-deletes)[ Packagist](https://packagist.org/packages/shiftonelabs/laravel-cascade-deletes)[ Docs](https://github.com/shiftonelabs/laravel-cascade-deletes)[ RSS](/packages/shiftonelabs-laravel-cascade-deletes/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (6)Versions (11)Used By (2)

laravel-cascade-deletes
=======================

[](#laravel-cascade-deletes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d096922027c0a1d61e643cef565923c675cdac66a721e8708e01990c6e2bff3c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73686966746f6e656c6162732f6c61726176656c2d636173636164652d64656c657465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/shiftonelabs/laravel-cascade-deletes)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)[![Build Status](https://camo.githubusercontent.com/c9d9509fc9de22ad9b2e497062c10fe212a4ed59040c95ca6f55660097d82a9f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73686966746f6e656c6162732f6c61726176656c2d636173636164652d64656c657465732f2e6769746875622f776f726b666c6f77732f706870756e69742e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/shiftonelabs/laravel-cascade-deletes/actions)[![Coverage Status](https://camo.githubusercontent.com/5cfb39caf2d731c22c978bd0a64bdc44c0c2be710025a1923503a055844b26e8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f73686966746f6e656c6162732f6c61726176656c2d636173636164652d64656c657465732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/shiftonelabs/laravel-cascade-deletes/code-structure)[![Quality Score](https://camo.githubusercontent.com/9c6f60c3d25343a899eb1586857515e4b651482547e73bff7260c937aace15d9/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f73686966746f6e656c6162732f6c61726176656c2d636173636164652d64656c657465732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/shiftonelabs/laravel-cascade-deletes)[![Total Downloads](https://camo.githubusercontent.com/316a48b49000fbdcd68d4dcf04e2f4f9950f676924d25488294e8d6c44616c9d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73686966746f6e656c6162732f6c61726176656c2d636173636164652d64656c657465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/shiftonelabs/laravel-cascade-deletes)

This Laravel/Lumen package provides application level cascading deletes for the Laravel's Eloquent ORM. When referential integrity is not, or cannot be, enforced at the data storage level, this package makes it easy to set this up at the application level.

For example, if you are using `SoftDeletes`, or are using polymorphic relationships, these are situations where foreign keys in the database cannot enforce referential integrity, and the application needs to step in. This package can help.

Versions
--------

[](#versions)

This package has been tested on Laravel 4.1 through Laravel 11.x, though it may continue to work on later versions as they are released. This section will be updated to reflect the versions on which the package has actually been tested.

This readme has been updated to show information for the most currently supported versions (9.x - 11.x). For Laravel 4.1 through Laravel 8.x, view the 1.x branch.

Install
-------

[](#install)

Via Composer

```
composer require shiftonelabs/laravel-cascade-deletes
```

Usage
-----

[](#usage)

Enabling cascading deletes can be done two ways. Either:

- update your `Model` to extend the `\ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletesModel`, or
- update your `Model` to use the `\ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes` trait.

Once that is done, define the `$cascadeDeletes` property on the `Model`. The `$cascadeDeletes` property should be set to an array of the relationships that should be deleted when a parent record is deleted.

Now, when a parent record is deleted, the defined child records will also be deleted. 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. This will continue on until all children, grandchildren, great grandchildren, etc. are deleted.

Additionally, all cascading deletes are performed within a transaction. This makes the delete an "all or nothing" event. If, for any reason, a child record could not be deleted, the transaction will rollback and no records will be deleted at all. 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

[](#code-example)

User Model:

```
