Laravel middleware with multiple roles

Laravel middleware with multiple roles

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Role-Based Access Control in Laravel Middleware

Building a multi-role access control system in Laravel often leads to confusion when trying to map hierarchical permissions directly onto middleware groups. A common misconception, illustrated by attempts to define approver+ groups containing stacked middleware for Editors and Admins, is that middleware acts as a cumulative permission list. In reality, the request passes through every middleware class defined in a group sequentially. If the first check fails, execution stops immediately; subsequent checks never run.

To implement a system where an Approver can access certain views while an Editor can also create content, and an Admin can do everything, you should rely on Laravel’s built-in route-specific middleware rather than trying to construct complex middleware groups for every role hierarchy.

The fundamental issue in the approach shown above is the structure of the middlewareGroups. When you define 'approver+' as a group containing \App\Http\Middleware\Approver, \App\Http\Middleware\Editor, and \App\Http\Middleware\Admin, a user with only Approver rights hits the first check. The Approver middleware passes them through, but then they hit the Editor middleware. Since the user isn't an Editor, that middleware redirects them to the login page before they ever reach the intended route logic.

The solution is to ensure that each middleware class checks only for its specific minimum requirement or higher, or more simply, to apply the appropriate single middleware to each route based on who needs to access it.

Defining Role Checks in Middleware

Your Http\Middleware\Admin class correctly identifies admins but fails to account for users who are not admins but might belong to other roles if they hit that specific middleware stack. However, a cleaner pattern is to ensure each middleware guards its own specific capability without assuming the presence of higher-level checks.

Update your middleware classes to be strict about their specific authorization level, or refactor them to allow access for anyone with "higher" privileges. For example:

// app/Http/Middleware/Approver.php
public function handle($request, Closure $next)
{
    if (Auth::check() && ($this->isAdmin() || $this->isApprover())) {
        return $next($request);
    }

    return redirect('/login'); // Or your custom unauthorized response
}

private function isAdmin(): bool
{
    if (!Auth::check()) return false;
    return Auth::user()->role_id === 4;
}

private function isApprover(): bool
{
    if (!Auth::check()) return false;
    return Auth::user()->role_id === 2;
}

By checking for both the specific role and higher roles within the handle method, you create a chain of trust. This means you can register just Approver middleware on routes that need "Approver or above" access without needing to stack Editor and Admin middleware classes in the same group.

Applying Middleware to Routes

Instead of creating a global 'approver+' group that forces all three classes onto every route, assign the single necessary middleware to the specific routes where it is required. This keeps your Kernel.php clean and your logic predictable.

In your routes/web.php, map the permissions directly:

// Routes accessible by Approver or higher (Admin/Editor)
Route::get('admin/scholen/overzicht', 'SchoolsController@overview')
    ->middleware(['auth', 'approver']); 

Route::get('admin/scholen/maken', 'SchoolsController@create')
    ->middleware(['auth', 'editor']);

Route::post('scholen', 'SchoolsController@store')
    ->middleware(['auth', 'editor']);

// Routes accessible only by Admin
Route::patch('admin/scholen/{id}', 'SchoolsController@update')
    ->middleware(['auth', 'admin']);

Notice how overview uses the approver middleware. Because the Approver middleware logic now checks for role_id === 2 (Approver) or role_id === 4 (Admin), a logged-in Approver will pass, and an Admin will also pass. An Editor, however, might need a separate check if they shouldn't see the overview, but based on your hierarchy where "Editor" implies access to everything previous plus more, you might actually want the approver middleware to handle the logic for anyone with role 2 or higher, and perhaps have a separate editor middleware that checks for role 3 or higher.

The Hierarchy Strategy

The most efficient way to handle this specific hierarchy (Student < Approver < Editor < Admin) without writing custom logic in every single route is to define your middleware to check for the minimum required role, but allow access for any role above that threshold.

If you want overview accessible by Approver, Editor, and Admin:

  1. Register a middleware class (e.g., ApproverOrHigher).
  2. Inside handle(), check if the user is an Approver, Editor, or Admin.
  3. If true, pass to the next route.
  4. Apply only this single middleware to the overview route.

This avoids the complexity of defining groups like 'approver+' in Kernel.php, which are often misunderstood as "cumulative permission sets" rather than "sequential gatekeepers." By keeping the middleware stack flat and assigning specific guards to routes, you maintain clarity. If a user logs in with Approver rights, they pass the ApproverOrHigher check. They do not hit an Editor middleware that would reject them.

This approach scales better as your roles change. You don't have to constantly update groups; you just adjust the logic inside the single guard class and reassign it to the relevant routes.

Our network