Laravel 5.1全球范围

时间:2016-03-11 10:42:20

标签: laravel laravel-5

我有一个全局范围,可以从查询中排除已停用的用户(1 - 已激活,0已取消激活)。但是我在检索这个已停用的用户时遇到了麻烦。我想创建一个与withTrashed()方法相同的函数。但我无法弄清楚如何。我应该在remove方法中写什么?

UsersScope类

class UsersScope implements ScopeInterface
{
    public function apply(Builder $builder, Model $model)
    {
        return $builder->where('status', '=', 1);
    }

    public function remove(Builder $builder, Model $model)
    {

    }

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

public function remove(Builder $builder, Model $model)
{

    $query = $builder->getQuery();
    $query->wheres = collect($query->wheres)->reject(function ($where) {
        return $where['column'] = 'status';
    })
    ->values()->all();
}

然后使用withoutGlobalScope方法删除范围:

$users = User::withoutGlobalScope(UsersScope::class)->get();