cesargb / laravel-cascade-delete
Cascading eliminations implemented in polymorphic relationships for the Laravel apps
Installs: 40 889
Dependents: 1
Suggesters: 0
Security: 0
Stars: 16
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/events: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- larastan/larastan: ^2.0
- laravel/legacy-factories: ^1.0.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.5
README
Cascading eliminations implemented in polymorphic relationships for the Laravel apps
This package permit add a trait for use in Elocuents Models that deletes in
cascade the Polymorphic Relations (MorphOne
, MorphMany
or MorphToMany
).
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 trait Cesargb\Database\Support\CascadeDelete
in your Elocuent Model and define de property cascadeDeleteMorph
whith one String or Array with your methods than define the Polymorphic Relations.
Code Sample
<?php namespace App; use App\Tag; use App\Image; use App\Option; use Illuminate\Database\Eloquent\Model; use Cesargb\Database\Support\CascadeDelete; class Video extends Model { use CascadeDelete; protected $cascadeDeleteMorph = ['image', 'tags', 'options']; public function image() { return $this->morphOne(Image::class, 'imageable'); } public function options() { return $this->morphMany(Option::class, 'optionable'); } public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
Now you can delete an App\Video
record, and any associated App\Image
, App\Options
and pivot records for
App\Tag
will be deleted.
Delete Residuals
If you bulk delete a model with morphological relationships, you will have residual data that has not been deleted.
To clean this waste you have the method deleteMorphResidual
Sample:
Video::query()->delete(); $video = new Video; $video->deleteMorphResidual();
Command to remove all residuals
You can use Artisan command morph:clean
to remove all residuals data from all
your Moldes that used the Cesargb\Database\Support\CascadeDelete
trait.
php artisan morph:clean
Contributing
Any contributions are welcome.