Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pedroroccon's avatar

Disable global query scope on specific controller

Greetings,

I created a global query scope in one of my models. The scope is called "waiting", and it is used to indicate that a follow resource is waiting to be approved.

    protected static function boot()
    {
        static::addGlobalScope('waiting', function(Builder $builder) {
            $builder->where('waiting', false);
        });
    }

I separated the approved resources in one specific view wich is associated to a controller. Then I created another controller wich will display all the "waiting" resources.

I know that I can disable the global scope temporarily, but there's a way to disable the global query scope in all methods of my controller. Later I'll need to implement more methods?

Regards!

0 likes
3 replies
Snapey's avatar

as far as I know, you have to add 'withoutGlobalScope' on each Eloquent call.

If you find you are needing to do this a lot then perhaps the code to load the model should be abstracted to a single method.

Alternatively (but less attractive) create a model that does not implement the scope. Have your scoped model extend the unscoped model.

srinathdudi's avatar
Level 15

You can create your model instance without the global scope inside the controllers constructor and use it like

class SomeController extends Controller
{
    protected $model;

    public function __construct()
    {
        $this->model = Model::withoutGlobalScope('waiting');
    }

    public function index()
    {
        $this->model->get();
    }
}
2 likes
pedroroccon's avatar

@snapey and @srinathdudi Thanks for the reply!

I liked the two solutions, but I liked the @srinathdudi solution. Simply adds a protected variable in the controllers that needs to disable the global query scope.

Thank you! Regards!

1 like

Please or to participate in this conversation.