kovabazs / laravel-restrict-soft-deletes
限制实现软删除的Eloquent模型删除。
dev-master
2018-11-13 16:21 UTC
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: 2024-09-14 04:58:50 UTC
README
限制实现软删除的Eloquent模型删除。基于Michael Dyrynda在https://dyrynda.com.au/blog/cascading-soft-deletes-with-laravel-and-eloquent上的想法。
安装
运行 composer require kobalazs/laravel-restrict-soft-deletes
用法
- 在你的模型中使用RestrictSoftDeletes,该模型需要限制删除
- 在模型类中添加一个受保护的属性,列出要监视的Eloquent关系
- 如果在受限制的模型上尝试删除,则特性将抛出
Netpok\Database\Support\DeleteRestrictionException
异常(HTTP 403)
示例
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');
}
...
}
在这个示例中,如果一个用户有任何未软删除的帖子,则不能删除该用户。