Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cascading deletes for Eloquent models that implement soft deletes

License

NotificationsYou must be signed in to change notification settings

michaeldyrynda/laravel-cascade-soft-deletes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusLatest Stable VersionTotal DownloadsLicenseBuy us a tree

Introduction

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.

Code Samples

<?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.

Installation

This trait is installed viaComposer. To install, simply add to yourcomposer.json file:

$ composer require dyrynda/laravel-cascade-soft-deletes

Support

If 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!

Treeware

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

Stars

Watchers

Forks

Packages

No packages published

Contributors19

Languages


[8]ページ先頭

©2009-2025 Movatter.jp