Laravel - How to pass variables to middleware through route group?

In Laravel 6.x you have to do this add code like in your middleware

 public function handle($request, Closure $next,$module=null,$right=null)
    {
        dd($module,$right);
        return $next($request);
    }

your route code like this

Route::get('/department/add', 'DepartmentController@addNew')->middleware('ManualSec:abc,xyz');

In Kernel.php register your middleware in the section of $routeMiddleware like

'ManualSec' => \App\Http\Middleware\ManualSec::class,

by calling the rout using url in my case

http://local.pms.com:8080/department/add

it will result is like:

enter image description here

now you can code in your middleware


You can simply pass multiple arguments into the middleware using a colon. Use it like:

Route::group(['middleware' => 'checkUserLevel:some_value_of_level'], function () {
            // my routes
        });

Now, you can have this value inside your $level variable.

public function handle($request, Closure $next, $level)
{
    $user = Auth::user();
    if ($user->level > $level) {
        return redirect('testUrl');
    }

    return $next($request);
}

This would help.


Edit: 14 Dec 2018

You can also send multiple variables to middleware. You just need to seperate the values using a comma (,).

Route::group(['middleware' => 'checkUserLevel:some_value_of_level, one_more_value_to_send'], function () {
                    // my routes
});

And you will get the value one_more_value_to_send in the variable after $level in the middleware handler.

public function handle($request, Closure $next, $level, $another_value)
{
   $user = Auth::user();
   if ($user->level > $level) {
      return redirect('testUrl');
   }

   return $next($request);
}

For more details you can refer to: Passing parameters to Middleware in Laravel 5.1