Laravel 5.4 Zizaco /委托 - 用户软删除

时间:2017-09-03 22:25:18

标签: laravel laravel-5 laravel-5.4 entrust

如何防止用户软删除时分离角色?
$user->hasRole('subscriber') =>真正
$user->delete()
$user->hasRole('subscriber') =>假
$user->restore()
$user->hasRole('subscriber') =>假

1 个答案:

答案 0 :(得分:0)

查看EntrustUserTrait行69-80。

/**
 * Boot the user model
 * Attach event listener to remove the many-to-many records when trying to delete
 * Will NOT delete any records if the user model uses soft deletes.
 *
 * @return void|bool
 */
public static function boot()
{
    parent::boot();
    static::deleting(function($user) {
        if (!method_exists(Config::get('auth.model'), 'bootSoftDeletes')) {
            $user->roles()->sync([]);
        }
        return true;
    });
}

如果您没有 bootSoftDeletes ,我认为您不使用Laravel自己的SoftDeletes特性。

class User extends Authenticatable
{
    use SoftDeletes;
    use EntrustUserTrait;
    ...