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

dmag's avatar
Level 6

User model global scope and Nova authorization issue

I want users to be able to ever see follow users only that belong to the same company as the logged in uder, so I've added a global scope to the User model.

class CompanyScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        $builder->where('company_id', auth()->user()->company_id);
    }
}

#[ScopedBy([CompanyScope::class])]
class User extends Authenticatable
{
    ...
}

However, this renders user unable to login into Nova as it's unable to authorize the user due to this error:

CompanyScope.php:15 Attempt to read property "company_id" on null

I would be able to solve it by using global filter on User resource in Nova instead of the global scope, but I need that scope in other parts of the application, not only in Nova.

How do I disable User model's global scope for Nova authorization only?

0 likes
0 replies

Please or to participate in this conversation.