litvinjuan / laravel-cascade-soft-deletes
Cascades soft delete and restored operations to the defined relationships
Fund package maintenance!
litvinjuan
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
This package is for cascading soft delete and restore operations on your related models. When a model gets soft deleted, the configured related models get soft deleted as well. When the original model is restored, its soft-deleted related models algo get restored.
Installation
You can install the package via composer:
composer require litvinjuan/laravel-cascade-soft-deletes
You can publish the config file with:
php artisan vendor:publish --tag="laravel-cascade-soft-deletes-config"
Soft Deleting
Simply add the trait to your models and configure the cascading relations either via the cascadeSofDeleteRelations
property or the getRelationsForCascadeSoftDeletes
method.
If your cascading related model also has the CascadeSoftDeletes
trait, its related models will also be soft-deleted.
In the following example, soft deleting a project, will also soft delete its tasks. And soft deleting a team, will soft delete its projects, and in turn, all their respective tasks will also be soft deleted.
class Team extends Model { use CascadeSoftDeletes; protected $cascadeSofDeleteRelations = ['projects']; public function projects() { return $this->hasMany(Project::class); } } class Project extends Model { use CascadeSoftDeletes; protected $cascadeSofDeleteRelations = ['tasks']; public function team() { return $this->belongsTo(Team::class); } public function task() { return $this->hasMany(Task::class); } } class Task extends Model { public function project() { return $this->belongsTo(Project::class); } }
Restoring
Restoration works very similarly. By default, it restores the same relations that were soft deleted, but you can customize the relations to be restored by setting the cascadeRestoreRelations
or implementing the getRelationsForCascadeRestore
.
If you want to disable restoration all together, go into the cascade-soft-deletes
config and set cascade_restores
to false
.
By default, when restoring related models, the package will only restore models that were deleted at the same time or after the parent model. This is to make sure we don't restore a child model that was soft deleted individually in a previous time. If you want to restore all models regardless of when they were soft deleted, you can go into the configuration file and set ignore_deleted_at_when_restoring
to true
.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.