0

I am working with Laravel version 8, and I want to use the global scope to add a where clause to all queries of the User model.

/**
* The "booting" method of the model.
* @return void
*/
protected static function boot()
{
    parent::boot();
    if (!empty(Client::$Current_Token)) {
        static::addGlobalScope(new ClientTokenScope);
    }
}

Main Problem: The issue I'm facing is that the variable Client::$Current_Token I want to add to this global scope is not set when executing the boot method. This is because this variable is set in a middleware.

How can I resolve the issue so that when executing the boot method, the client token is set and not empty?

1 Answer 1

0

You can try using the booted method instead of the boot method. booted method is called after all of the booting is completed and you can ensure that value is being set in middleware for Client::$Current_Token.

public function booted()
{
    if (!empty(Client::$Current_Token)) {
        static::addGlobalScope(new ClientTokenScope);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't work for me and i don't know why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.