// Middleware to check if the user is an admin
public function handle($request, Closure $next)
{
// Verify the user is authenticated and an admin
if (auth()->check() && auth()->user()->isAdmin) {
return $next($request);
}
// Redirect non-admin users to the home page
return redirect()->route('home');
}
code
Middleware to Restrict Access to Admins in Laravel
This Laravel middleware checks if the user is authenticated and is an admin. If the conditions are met, the request is allowed to proceed; otherwise, the user is redirected to the 'home' page. The comments provide explanations for each part of the code.
Middleware
isAdmin