qr11
qr11

Reputation: 441

Laravel 5.1 Global scope

I have a global scope that exclude deactivated users from query(1 - Activated, 0-Deactivated). But I have troubles with retrieving this deactivated users. I want to create a function that doing the same as withTrashed() method. But i cant figure out how. What should I write in remove method?

UsersScope class

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

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

    }

Upvotes: 2

Views: 1182

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You may try this:

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

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

Then use withoutGlobalScope method to remove the scope:

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

Upvotes: 1

Related Questions