Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Cascade Delete Laravel Package

cesargb/laravel-cascade-delete

View on GitHub
Deep Wiki
Context7

tests static analysis lint 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope