A drop-in trait that extends Laravel soft deletes so related models are deleted and restored automatically.
- Automatic cascading of soft deletes and restores
- Works with pivot tables that store a
deleted_at
column - Jobs and chunking for large datasets or async processing
- Fires events during delete and restore cycles
- Artisan commands to manage cascaded records
- Configurable chunk size, queue connection and strategy
composer require andreaskviby/CascadeSoftDeletes-Pro
Publish the configuration file:
php artisan vendor:publish --tag=cascadepro-config
Key options in config/cascadepro.php
:
default_strategy
– process records synchronously or through the queuechunk_size
– number of models processed before jobs are chunkedqueue_connection
– queue connection used when dispatching jobspivot_tables
– pivot tables containing soft delete columns
Apply the trait to models and list which relations should cascade.
use Stafe\CascadePro\CascadeSoftDeletes;
class Post extends Model
{
use SoftDeletes, CascadeSoftDeletes;
protected array $cascadeDeletes = ['comments'];
}
The package emits events you can listen for:
DeletingCascade
andDeletedCascade
RestoringCascade
andRestoredCascade
php artisan cascade:flush {model} # hard delete soft-deleted trees
php artisan cascade:scan # list models missing cascade mapping
Contributions are welcome! Feel free to open issues or submit pull requests.
This package is maintained by Andreas Kviby. I enjoy building tools for the Laravel community and appreciate any feedback.
The MIT License. See LICENSE.md for details.