iatstut/laravel-cascade-soft-delete

Cascading deletes for Eloquent models that implement soft deletes

dev-master 2018-05-12 12:56 UTC

This package is not auto-updated.

Last update: 2024-05-06 05:40:27 UTC


README

v1.4.0

Travis Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

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 an ON 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's soft 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 the SoftDeletes trait.

As of v1.0.2, you can inherit CascadeSoftDeletes from a base model class, you no longer need to use the trait on each child if all of your models happen to implement SoftDeletes.

As of v1.0.4, the package supports cascading deletes of grandchildren records (#8, #9).

As of v1.0.5, the package has better support of hasOne relationships (#10, #11).

v1.1.0 adds compatibility with Laravel 5.3.

v1.2.0 adds compatibility with Laravel 5.4.

v1.3.0 adds compatibility with Laravel 5.5.

v1.4.0 adds compatibility with Laravel 5.6.

Code Samples

<?php

namespace App;

use App\Comment;
use Iatstuti\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $cascadeDeletes = ['comments'];

    protected $dates = ['deleted_at'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

Now you can delete an App\Post record, and any associated App\Comment records will be deleted. If the App\Comment record implements the CascadeSoftDeletes 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 the deleting Eloquent model event, we can prevent the parent record from being deleted as well as any child records, if any exception is triggered. A LogicException will be triggered if the model does not use the Illuminate\Database\Eloquent\SoftDeletes trait, or if any of the defined cascadeDeletes relationships do not exist, or do not return an instance of Illuminate\Database\Eloquent\Relations\Relation.

Additional Note: If you already have existing event listeners in place for a model that is going to cascade soft deletes, you can adjust the priority or firing order of events to have CascadeSoftDeletes fire after your event. To do this you can set the priority of your deleting event listener to be 1.

MODEL::observe( MODELObserver::class, 1 ); The second param is the priority.

MODEL::deleting( MODELObserver::class, 1 );

As of right now this is not documented in the Larvel docs, but just know that the default priority is 0 for all listeners, and that 0 is the lowest priority. Passing a param of greater than 0 to your listener will cause your listener to fire before listeners with default priority of 0

Installation

This trait is installed via Composer. To install, simply add to your composer.json file:

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

Support

If you are having general issues with this package, feel free to contact me on Twitter.

If you believe you have found an issue, please report it using the GitHub 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!