Automatically soft delete related models when the parent model is soft deleted and restore them when the parent model is restored.
⚠️ Restoring is experimental. All models whose deleted_at timestamp is greater than the parent model's deleted_at timestamp will be restored.
Add this repository to your composer.json
file:
{
"repositories": [
{
"type": "github",
"url": "https://github.com/mintellity/laravel-cascade-soft-deletes.git"
}
]
}
You can install the package via composer:
composer require mintellity/laravel-cascade-soft-deletes
Add the Mintellity\LaravelCascadeSoftDeletes\Traits\CascadeSoftDeletes
trait to your model. You can remove the Illuminate\Database\Eloquent\SoftDeletes
trait if you want to. Add each relation you want to cascade soft delete, to the cascadeDeletes
array in your Model.
class User extends Model
{
use CascadeSoftDeletes;
protected array $cascadeDeletes = ['orders'];
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
}
To also cascade force deletes and the deleted event, also add the Mintellity\LaravelCascadeSoftDeletes\Traits\CascadeForceDeletes
trait to your model.
class User extends Model
{
use CascadeSoftDeletes,
CascadeForceDeletes;
protected array $cascadeDeletes = ['orders'];
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
}
To also cascade restore, also add the Mintellity\LaravelCascadeSoftDeletes\Traits\CascadeRestores
trait to your model. By default the cascadeRestores
array will be the same as the cascadeDeletes
array. If you want to restore other relations, you can add them to the cascadeRestores
array.
class User extends Model
{
use CascadeSoftDeletes,
CascadeRestores;
protected array $cascadeDeletes = ['orders'];
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
}
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.