askedio / laravel5-soft-cascade
Laravel Cascade Soft Delete & Restore
Installs: 80 387
Dependents: 0
Suggesters: 0
Security: 0
Stars: 706
Watchers: 16
Forks: 64
Open Issues: 9
Requires
- php: ^8.2
- illuminate/container: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/events: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0|^11.0
- dev-master
- 11.0.4
- 11.0.3
- 11.0.2
- 11.0.1
- 11.0.0
- 10.0.x-dev
- 10.0.0
- 9.0.x-dev
- 9.0.0
- 8.1.0
- 8.0.x-dev
- 8.0.0
- 7.0.x-dev
- 7.0.2
- 7.0.1
- 7.0.0
- 6.0.x-dev
- 6.0.1
- 6.0.0
- 5.8.x-dev
- 5.8.0
- 5.7.x-dev
- 5.7.0
- 5.6.x-dev
- 5.6.5
- 5.6.4
- 5.6.3
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.x-dev
- 5.5.17
- 5.5.16
- 5.5.15
- 5.5.14
- 5.5.13
- 5.5.12
- 5.5.11
- 5.5.10
- 5.5.9
- 5.5.8
- 5.5.7
- 5.5.6
- 5.5.5
- 5.5.4
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0
- 1.8
- 1.7
- 1.6
- 1.5
- 1.4
- 1.3
- 1.2
- 1.1
- 1.0
- dev-fix/cleanup
- dev-145-forcedelete-functionality-added
- dev-fix/replace-try-catch-with-is_callable
- dev-revert-155-fix/replace-try-catch-with-method_exists
This package is auto-updated.
Last update: 2024-10-28 13:30:33 UTC
README
Laravel/Lumen Soft Cascade Delete & Restore
Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.
Why do I need it?
To make soft deleting and restoring relations easy.
If you enjoy features like MySQL cascade deleting but want to use Laravels SoftDeletes feature you'll need to do some extra steps to ensure your relations are properly deleted or restored.
This package is intended to replace those steps with a simple array that defines the relations you want to cascade.
Installation
Install with composer
composer require askedio/laravel-soft-cascade
From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php
Askedio\SoftCascade\Providers\GenericServiceProvider::class,
Lumen does not support the auto-discovery feature, you should manually add the provider.
Askedio\SoftCascade\Providers\LumenServiceProvider::class,
Usage
In your Model
enable the trait and define $softCascade
. Example.
use \Askedio\SoftCascade\Traits\SoftCascadeTrait;
protected $softCascade = ['profiles'];
For restricted relation use. Example.
use \Askedio\SoftCascade\Traits\SoftCascadeTrait;
protected $softCascade = ['addresses@restrict'];
$softCascade
is an array of your relation names, in the example you'll see we've defined function profiles()
for the relation.
Nested relations work by defining $softCascade
in the related Model
as you can see here.
After you've defined your relations you can simply trigger delete()
or restore()
on your Model
and your relations will have the same task performed.
User::first()->delete();
User::withTrashed()->first()->restore();
It can also be used with query builder in this way because query builder listener is executed after query, we need to use transaction for rollback query on error due to restricted relationships
try {
DB::beginTransaction(); //Start db transaction for rollback query when error
User::limit(2)->delete();
User::withTrashed()->limit(2)->restore();
DB::commit(); //Commit the query
} catch (\Exception $e) {
DB::rollBack(); //Rollback the query
//Optional, if we need to continue execution only rollback transaction and save message on variable
throw new \Askedio\SoftCascade\Exceptions\SoftCascadeLogicException($e->getMessage());
}
Supported Databases
- MySQL
- PostgreSQL
- SQLite
- SQL Server
Testing
I have written some very basic tests, certainly more needs to be done here. If you find this useful please help by testing other databases or writing better unit tests because I must move on.
Issues & Contributing
I will be using this with MySQL in a new API so any issues I find related to my use will be resolved. If you find an issue with MySQL please report it and I will fix it.
If you are using another database and have issues please contribute by submitting a pull request. I do not have time to test this with other database but assume all would work.