Skip to content

Latest commit

 

History

History
93 lines (63 loc) · 2.78 KB

README.md

File metadata and controls

93 lines (63 loc) · 2.78 KB

tests phpstan style-fix Quality Score Total Downloads

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.