- Notifications
You must be signed in to change notification settings - Fork84
Cascading deletes for Eloquent models that implement soft deletes
License
michaeldyrynda/laravel-cascade-soft-deletes
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
In scenarios when you delete a parent record - say for example a blog post - you may want to also delete any comments associated with it as a form of self-maintenance of your data.
Normally, you would use your database's foreign key constraints, adding anON DELETE CASCADE rule to the foreign key constraint in your comments table.
It may be useful to be able to restore a parent record after it was deleted. In those instances, you may reach for Laravel'ssoft deleting functionality.
In doing so, however, you lose the ability to use the cascading delete functionality that your database would otherwise provide. That is where this package aims to bridge the gap in functionality when using theSoftDeletes trait.
<?phpnamespaceApp;useApp\Comment;useDyrynda\Database\Support\CascadeSoftDeletes;useIlluminate\Database\Eloquent\Model;useIlluminate\Database\Eloquent\SoftDeletes;class Postextends Model{use SoftDeletes, CascadeSoftDeletes;protected$cascadeDeletes = ['comments'];protected$dates = ['deleted_at'];protected$fetchMethod ='get';// get, cursor, lazy or chunk// protected $chunkSize = 500;publicfunctioncomments() {return$this->hasMany(Comment::class); }}
Now you can delete anApp\Post record, and any associatedApp\Comment records will be deleted. If theApp\Comment record implements theCascadeSoftDeletes trait as well, it's children will also be deleted and so on.
$post =App\Post::find($postId)$post->delete();// Soft delete the post, which will also trigger the delete() method on any comments and their children.
Note: It's important to know that when you cascade your soft deleted child records, there is no way to know which were deleted by the cascading operation, and which were deleted prior to that. This means that when you restore the blog post, the associated comments will not be.
Because this trait hooks into thedeleting Eloquent model event, we can prevent the parent record from being deleted as well as any child records, if any exception is triggered. ALogicException will be triggered if the model does not use theIlluminate\Database\Eloquent\SoftDeletes trait, or if any of the definedcascadeDeletes relationships do not exist, or do not return an instance ofIlluminate\Database\Eloquent\Relations\Relation.
This trait is installed viaComposer. To install, simply add to yourcomposer.json file:
$ composer require dyrynda/laravel-cascade-soft-deletesIf you are having general issues with this package, feel free to contact me onTwitter.
If you believe you have found an issue, please report it using theGitHub issue tracker, or better yet, fork the repository and submit a pull request.
If you're using this package, I'd love to hear your thoughts. Thanks!
You're free to use this package, but if it makes it to your production environment you are required to buy the world a tree.
It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
You can buy treeshere
Read more about Treeware attreeware.earth
About
Cascading deletes for Eloquent models that implement soft deletes
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.