Home > database >  define custom array in route using Laravel 8
define custom array in route using Laravel 8

Time:09-23

In previous version of Laravel 5 we define extra or custom array in route along multi middleware like below:

Route::get('/user_list',[
    'uses'=>'UserController@index',
    'as' =>'user_list',
    middleware' => ['auth','roles'],
    'roles' => ['Administrator', 'Operator']
   ]);

However, in Laravel 8 it gives error and I defined like below: as it gives error on 'uses'. But below syntax is not belong to Laravel 8.

Route::get('/user_list',array(
        'uses'=>'App\Http\Controllers\UserController@index',
        'as' =>'user_list',
        'middleware' => ['auth','roles'],
        'roles' => ['Administrator', 'Operator'])
);

My Main Question is to define custom array in route as per Laravel 8.

Route::get('user_list',[UserController::class, 'index'])
    ->middleware(['auth','roles'])
    ->name('user_list');

This above code works fine but I need to define an custom array in order to check the role 'roles' => ['Administrator', 'Operator']

CodePudding user response:

try following

Route::get('user_list',[UserController::class, 'index'])
    ->middleware(['auth','roles','role:Administrator|Operator'])
    ->name('user_list');

CodePudding user response:

You can simply define it about all routes and use it like this

$roles= ['uses'=>'App\Http\Controllers\UserController@index',
        'as' =>'user_list',
        'middleware' => ['auth','roles'],
        'roles' => ['Administrator', 'Operator']];

Route::get('/user_list', function () use ($roles) {   
    return anything;
});
  • Related