Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
Pass an array to a middleware in laravel

I got this handle in a middleware called rolMiddleware:

public function handle($request, Closure $next, $roles)
{
    //dd($request->user());
    foreach ($roles as $rol) {
        if ($request->user()->getTipoUsuario($request->user()->tipo_usuario_id)->getNombreTipoUsuario() == $rol) {
            return $next($request);
        }
    }
    abort(403, "¡No hay autorizacion!");
}

But $roles is an array, here is the route where I use the middleware:

Route::get('/mid', ['middleware' => 'roles:super admin', function () {
    return "done";
}]);

and the error that gives me is:

ErrorException in RolMiddleware.php line 22:
Invalid argument supplied for foreach()

You may thing that I do not need an array because I am only using it in super admin, for that route I only need super admin, but there would be routes that can be for super admin and the admin of an area.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In laravel , you can separate your parameters which you want to pass to middleware using comma , as follows:

Route::get('/mid', ['middleware' => 'roles:super,admin', function () {
//                                              ^ note this
    return "done";
}]);

note that, this won't send parameters as an array, so you can't loop over $roles unless you use your passed parameters as ellipsis parameters as follows :

public function handle($request, Closure $next, ...$roles)

rather, you will need to use a single parameter for each role:

public function handle($request, Closure $next, $role1, $role2) // .... and so on
over 4 years ago · Santiago Trujillo Report

0

Route:

Route::get('/access', ['middleware' => 'hasroles:super,admin', function () {
}]);

passing one parameter to check user have created permission in my cause

Route::middleware('admin')->namespace('Admin')->prefix('admin')->group(function(){
    Route::get('/home', 'MainController@getIndex')->name('admin.index')->middleware("hasrole:create");

Middleware:

1.Using Parameters

public function handle($request, Closure $next, $parm1, $parm2){}

2.Using var-arg

public function handle($request, Closure $next, $parm1, $parm2){}
public function handle($request, Closure $next, ...$parm1){}

Two-way Middleware Use

1: Register routeMiddleware

// Within App\Http\Kernel Class...
protected $routeMiddleware = [
    'hasrole' => \Illuminate\Auth\Middleware\HasRole::class,

Using:

Route::get('admin/profile', function () {
})->middleware('hasrole'); 

2: Not Register in routeMiddleware

Using:

use App\Http\Middleware\HasRole;

Route::get('admin/profile', function () {
    //
})->middleware(HasRole::class);
over 4 years ago · Santiago Trujillo Report

0

Send parameter as string like as

Route::prefix('panel')->middleware('auth:admin|editor')->group(function (){
    Route::get('/', [SiteController::class, 'index'])->name('site.index');
}

Program in middleware to sense this string as array

if (in_array(Auth::user()->rule, explode('|', $access))) {
   return $next($request);
} else {
   return redirect()->route('site.denied');
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!