0

I have problem to make routing with middleware multi roles I have tried some in internet but still wont work

I have 3 roles, superadmin, admin and member I want the superadmin and admin can access the add page

here is my code :

Route::group(['prefix' => 'staff', 'middleware' => 'auth'], function () {


    Route::GET('/add', [
        'uses'       => 'StaffController@page_add',
        'middleware' => 'rule:superadmin', ???
    ]);

});

I have tried to put 'middleware' => 'rule:superadmin|rule:admin' but wont work

thank you

Haroon nasir
  • 648
  • 1
  • 7
  • 21
saintadjie
  • 34
  • 2
  • 9

2 Answers2

0

Create a middleware file eg Role.php

public function handle($request, Closure $next, ... $roles)
{
    if (!Auth::check()) // I included this check because you have it, but it really should be part of your 'auth' middleware, most likely added as part of a route group.
        return redirect('login');

    $user = Auth::user();

    if($user->isAdmin())
        return $next($request);

    foreach($roles as $role) {
        // Check if user has the role This check will depend on how your roles are set up
        if($user->hasRole($role))
            return $next($request);
    }

    return redirect('login');
}

Finally in your web routes

Route::get('admin/scholen/overzicht', 'SchoolsController@overview')->middleware('role:editor,approver');
Route::get('admin/scholen/{id}/bewerken', 'SchoolsController@edit')->middleware('role:admin');

Check out this best answer for more details

Francis Tito
  • 126
  • 1
  • 9
0

Hey you can put a column named "role" in your users table then check it with a condition.

 Route::get('/add', function() {
    if (Auth::user()->role == 'superadmin' || Auth::user()->role == 'admin') {
      return view('add-page');
    } 
    else {
      return view('error-page');
    } 
  });
Tanvir Ahmed
  • 969
  • 12
  • 24