Level 1
I keep getting the error Forbidden. User does not have the right roles.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
my RoleMiddleware below does not return the user's roles
public function handle($request, Closure $next, $role, $guard = null)
{
if (Auth::guard($guard)->guest()) {
throw UnauthorizedException::notLoggedIn();
}
$roles = is_array($role)
? $role
: explode('|', $role);
if (! Auth::guard($guard)->user()->hasAnyRole($roles)) {
throw UnauthorizedException::forRoles($roles);
}
return $next($request);
}
I have added everything correctly to the route and I can't seem to understand why the roles connected to a particular user does not get returned.
Please or to participate in this conversation.