An open API service indexing awesome lists of open source software.

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

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
```