Level 70
@armani I may go with middleware instead of scope here.
Based on the user types/rights, I may enable or disable certain scopes.
I created a global scope by running this command:
php artisan make:scope BranchScope
And the logic it like this:
public function apply(Builder $builder, Model $model): void
{
$builder->where('branch_id', auth()->user()->branch_id);
}
then applied to the model like this:
#[ScopedBy(BranchScope::class)]
and everything works fine but I want to apply the scope only for regular users not for admin.
so I created a local scope like this:
public function scopeAdmin(Builder $query): void
{
if(auth()->user()->isAdmin)
{
$query->withoutGlobalScopes();
}
}
and Added to the query like this:
Item::query()->admin()->get();
and it works but I want to know, is there a better way to do it?
Please or to participate in this conversation.