exls/laravel-cascade-soft-deletes

Cascading queued or regular deletes and restores for Eloquent models that implement soft deletes

1.0.2 2019-07-10 12:10 UTC

This package is not auto-updated.

Last update: 2024-05-15 18:31:44 UTC


README

Introduction

In scenarios when you delete a parent record you may want to also delete any detail/child 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 detail/child table.

If you need to be able to restore a parent record after it was deleted, check you may reach for Laravel's soft deleting functionality.

But in this case you can use database feature to cascade delete details. This package support cascade soft deletes feature and support queues to make last one.

Installation

Pull this package in through Composer.

    composer require exls/laravel-soft-deletes

Laravel 5.* Integration

Add the service provider to your config/app.php file:

    'providers'  => array(

        //register listeners on events
        Exls\LaravelCascadeSoftDeletes\Providers\CascadeSoftDeletesServiceProvider::class,

    ),

Usage

Change SoftDeletes trait in your models to CascadeSoftDeletes trait from this package

<?php

namespace App\Models;

use App\Models\Master\Detail;
use Exls\LaravelCascadeSoftDeletes\Traits\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Master extends Model
{
    //Instead of SoftDeletes
    use CascadeSoftDeletes;

    //Remove immideately details
    protected $cascadeDeletes = ['details'];
    
    // or use queues to soft delete details
    protected $queuedCascadeDeletes = ['details'];

	protected $dates = ['deleted_at'];

    public function details()
    {
        return $this->hasMany(Detail::class);
    }
}    

Now you can delete an App\Models\Master record, and any associated App\Models\Master\Detail records will be deleted. If the App\Models\Master\Detail record use the CascadeSoftDeletes trait as well, it's details/children will also be deleted and so on.

    App\Models\Master::findOrFail($masterId)->delete()

Tests

Contact

Anton Pavlov