In our project, we have anonymous global scope defined this way:
static::addGlobalScope('user_team', function (Builder $builder) {
$builder->where('team_id', '=', auth()->user()->team_id);
}
It is used to show the user information related to his team only.
If we have a user, for example, not assigned to any team it gives a 404 error with a default "Not Found" message.
What I need is to have a kind of customized exception message "You are not assigned to any team".
While doing this under local scope it works this way:
public function scopeTeam() {
$myTeam = $this->where('team_id', auth()->user()->team_id)->first();
if (!$myTeam) {
abort(403, 'No team assigned');
}
return $myTeam;
}
I need the same under a global scope.
Reading the documentation about Global Scopes here https://laravel.com/docs/9.x/eloquent#query-scopes did not help.
Same as browsing the vendor/illuminate/... folder to see how it is implemented in general.
Any thoughts?