Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dauber's avatar

Passing parameters with middleware groups?

So...I'm using Laravel 12. I know that if you have plain ol' middleware, you can pass a parameter by doing this in your routes:

Route::get('/admin/users', [App\Http\Controllers\UserController::class, 'index'])->name('users.index')->middleware(RoleMiddleware::class . ':Admin');

But what if I have a group, and I call that group "roles"??? According to the docs, I'd do something like this:

Route::get('/admin/users', [App\Http\Controllers\UserController::class, 'index'])->name('users.index')->middleware('roles);

Or even:

Route::middleware(['roles'])->group(function() { // but the docs fail to explain how to translate the above to this }

One person suggested I do this:

Route::middleware(['roles', 'role:Admin'])->get('/admin/users', [App\Http\Controllers\UserController::class, 'index'])->name('users.index');

If I try that suggestion, I get the "Too few arguments" problem (2 passed in, 3 expected)

How do I do this??

0 likes
3 replies
Shivamyadav's avatar

Your middleware is currently set for 3 arguments while using and you are passing only 2 arguments..

Show me your middleware code here and tell me the relationship that you are using for users and roles

dauber's avatar

Well, yeah, I know it's currently set up for 3 arguments. I'm trying to figure out how to actually pass the third one -- It works if I take the middleware out of the group and use it individually -- I just can't figure out how to do it when it's in the group.

/* Middleware code: */

namespace App\Http\Middleware; use Closure; use Symfony\Component\HttpFoundation\Response;

class RoleMiddleware { public function handle(Request $request, Closure $next, $role): Response { if (!$request->user->userHasRole($role) { abort(403, 'Unauthorized'); }

    return $next($request);
}

}

/* Relationship, in the Role model (pardon the pun): */

public function users() { return $this->belongsToMany(User::class); }

/* Relationship in the User model */

public function roles() { return $this->belongsToMany(Role::class); }

dauber's avatar

That works, but how would I send it to the entire "roles" group? (or would that be redundant at this point?)

Please or to participate in this conversation.