danc81's avatar

Apply global scope to Scout search

Is there a way to apply a global scope to a Scout search?

I have a simple where scope which filters results based on the current user ID:

public function apply(Builder $builder, Model $model): void
{
    $builder->where('user_id', $this->user->id);
}

This is applied via middleware like

Item::addGlobalScope(new UserScope(auth()->user()));

however, the scope is applied to the Eloquent builder after the Scout search which results in the pagination being incorrect.

Is there a way to apply a global scope to the Scout builder before the search is performed? Or perhaps a better approach?

0 likes
0 replies

Please or to participate in this conversation.