I have two models with global scopes.

#[ScopedBy(UserScope::class)]
class User {}

#[ScopedBy(ViewerScope::class)]
class Viewer extends User {}

The scope just checks if class instance has is_viewer === true or not. How can I disable/ignore parent scope in child model? I tried both approaches with #[ScopedBy...] and applying it in a booted() model methods. No luck with both(

1 Reply 1

To ignore the parent scope in your child model, override it in the booted() method:

class Viewer extends User
{
    protected static function booted()
    {
        // Clear parent scopes and apply only viewer scope
        static::$globalScopes = [];
        static::addGlobalScope(new ViewerScope());
    }
}

Alternatively, use withoutGlobalScopes() in specific queries when needed.

Your Reply

By clicking “Post Your Reply”, 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.