kobalazs / laravel-restrict-soft-deletes
Restrict deletes for Eloquent models that implement soft deletes.
Installs: 2 957
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:utility
Requires
- php: >=5.4
- illuminate/database: ~5.0
- illuminate/events: ~5.0
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is auto-updated.
Last update: 2025-06-14 06:37:33 UTC
README
Restrict deletes for Eloquent models that implement soft deletes. Based on the idea of Michael Dyrynda at https://dyrynda.com.au/blog/cascading-soft-deletes-with-laravel-and-eloquent
Installation
Run composer require kobalazs/laravel-restrict-soft-deletes
Usage
- Use RestrictSoftDeletes in your Model you want to have restricted
- Add a protected attibute to your Model class listing the Eloquent relationships to be watched
- In case of an attempted deletion on a restricted model the trait will throw a
Netpok\Database\Support\DeleteRestrictionException
(HTTP 403)
Example
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Netpok\Database\Support\RestrictSoftDeletes;
class User extends Model
{
use SoftDeletes;
use RestrictSoftDeletes;
/**
* The relations restricting model deletion
*/
protected $restrictDeletes = ['posts'];
/**
* Eloquent relationship (has to be defined for RestrictSoftDeletes to work!)
*/
public function posts()
{
return $this->hasMany('App\Post');
}
...
}
In this example a User can not be deleted while they have any Post that is not soft-deleted.