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 polymorphic relations

License

NotificationsYou must be signed in to change notification settings

cesargb/laravel-cascade-delete

Repository files navigation

testsphpstanstyle-fixQuality ScoreTotal Downloads

Cascading eliminations implemented in polymorphic relationships for the Laravel apps

This package permit add a trait for use in Elocuents Models that deletes incascade the Polymorphic Relations (MorphOne,MorphMany orMorphToMany).

Installation

This package can be used in Laravel 5.5 or higher.

You can install the package via composer:

composer require cesargb/laravel-cascade-delete

Use

Use the traitCesargb\Database\Support\CascadeDelete in your Elocuent Model and define de propertycascadeDeleteMorph whith one String or Array with your methods than define the Polymorphic Relations.

Code Sample

<?phpnamespaceApp;useApp\Tag;useApp\Image;useApp\Option;useIlluminate\Database\Eloquent\Model;useCesargb\Database\Support\CascadeDelete;class Videoextends Model{use CascadeDelete;protected$cascadeDeleteMorph = ['image','tags','options'];publicfunctionimage()    {return$this->morphOne(Image::class,'imageable');    }publicfunctionoptions()    {return$this->morphMany(Option::class,'optionable');    }publicfunctiontags()    {return$this->morphToMany(Tag::class,'taggable');    }}

Now you can delete anApp\Video record, and any associatedApp\Image,App\Options and pivot records forApp\Tag will be deleted.

Delete Residuals

If you bulk delete a model with morphological relationships, you will haveresidual data that has not been deleted.

To clean this waste you have the methoddeleteMorphResidual

Sample:

Video::query()->delete();$video =newVideo;$video->deleteMorphResidual();

Command to remove all residuals

You can use Artisan commandmorph:clean to remove all residuals data from allyour Moldes that used theCesargb\Database\Support\CascadeDelete trait.

php artisan morph:clean

Contributing

Any contributions are welcome.

About

Cascading deletes for Eloquent models that implement polymorphic relations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp