exls / laravel-cascade-soft-deletes
Cascading queued or regular deletes and restores for Eloquent models that implement soft deletes
Installs: 1 367
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Type:utility
Requires
- php: >=5.4
- illuminate/database: ~5.8.0
- illuminate/events: ~5.8.0
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2025-02-19 22:18:05 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
- Email: anton.pavlov.it@gmail.com