mintellity / laravel-cascade-soft-deletes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cascade Soft Deletes for relations in your Laravel models.

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.

Installation

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

Usage

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);
    }
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

License:MIT License


Languages

Language:PHP 97.9%Language:Shell 2.1%