https://github.com/imarc/cascade
Cascading soft deletes for Laravel Eloquent models
https://github.com/imarc/cascade
Last synced: 26 days ago
JSON representation
Cascading soft deletes for Laravel Eloquent models
- Host: GitHub
- URL: https://github.com/imarc/cascade
- Owner: imarc
- Created: 2026-04-14T11:42:02.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2026-04-14T12:40:53.000Z (3 months ago)
- Last Synced: 2026-04-14T13:30:35.986Z (3 months ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cascade
Cascading soft deletes for Laravel Eloquent. When a parent model is soft-deleted, related models are soft-deleted. When the parent is restored, **all** currently trashed rows for those relations are restored one-by-one so `restored` events run (nested cascades work). This still restores every trashed child for that foreign key, not only rows deleted in the same cascade as the parent.
## Usage
Use Laravel’s `SoftDeletes` and this package’s `CascadeSoftDeletes` on the parent. List relationship method names to cascade:
```php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Imarc\Cascade\CascadeSoftDeletes;
class Post extends Model
{
use CascadeSoftDeletes, SoftDeletes;
/** @var list */
protected array $cascadeSoftDeletes = ['comments'];
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}
```
Child models should use `SoftDeletes` if you want them included; non–soft-delete children are skipped on cascade delete.
## Supported relations
`HasMany`, `HasOne`, `MorphMany`, and `MorphOne` are supported. `BelongsTo`, `BelongsToMany`, and other relation types are ignored for cascading.
## Force delete
When the parent is force-deleted, soft-deletable children in the listed relations are force-deleted so orphaned soft-deleted rows are not left behind.
## Install
```bash
composer require imarc/cascade
```
## Tests (from application root)
```bash
php artisan test --testsuite=Cascade
```
Or:
```bash
./vendor/bin/phpunit -c packages/imarc/cascade/phpunit.xml
```