skip to Main Content

Laravel-8 with the Spatie permission package. I have already assigned role and permissions to my users. My problem is how can I route my users after logging in to a path according to their role?

I have the following roles: Admin, Manager, Administrative, Teacher, Student. I want your route to be something like this:

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified', 'role:Admin']], function() {
  // ...
});

Route::group(['prefix' => 'manager', 'middleware' => ['auth', 'verified', 'role:Manager']], function() {
  // ...
});

Route::group(['prefix' => 'teacher', 'middleware' => ['auth', 'verified', 'role:Teacher']], function() {
  // ...
});

Route::group(['prefix' => 'student', 'middleware' => ['auth', 'verified', 'role:Student']], function() {`
  // ...
});

2

Answers


  1. Chosen as BEST ANSWER

    In HomeController add this in the index method:

    public function index()
    {
        $userCurrent = auth()->user()->getRoleNames()->first();
        
        if ($userCurrent === auth()->user()->hasRole('Directivo')) {
            return redirect('dirtvo/dashboard');
        } else if ($userCurrent === auth()->user()->hasRole('Administrativo')) {
            return redirect('/adtvo/dashboard');
        } else if ($userCurrent === auth()->user()->hasRole('Docente')) {
            return redirect('/prof/dashboard');
        } else if ($userCurrent === auth()->user()->hasRole('Estudiante')) {
            return redirect('/est/dashboard');
        } else {
            return redirect('/adm/dashboard');
        }
        #return view('home');
    }
    

Please signup or login to give your own answer.
Back To Top
Search