N
N
Northern Lights2020-02-28 10:20:51
Laravel
Northern Lights, 2020-02-28 10:20:51

What is the best way to bypass the global scope in code?

Good afternoon.
There is a banal global scope that works for several related models:

abstract class ....
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('is_not_archive', function (Builder $builder) {
            $builder->where('archive', '=', 0);
        });
    }


Accordingly, when requesting $object->relation in the SQL query, in addition to the relation expression, the expression " archive = 0 " is substituted.

Now we need to use the $object->relation, but we need to invert the scope, select records with " archive = 1 ".

How to do it in the most correct and beautiful way, without shitty code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Popov, 2020-02-28
@php666

The local scope, similarly to the global one, is bypassed through withoutGlobalScope(). In your context - You can see more information in the official laravel documentation in the chapter on scopes . Post::withoutGlobalScope("is_not_archive")->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question