Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afrasiyabhaider's avatar

How to create route of login on the basis of role column?

I've created a column named as 'role' in Users table there are 3 roles student, teacher, admin I want that when the user enters his credentials he should be redirected to his home page e.g if email = [email protected] password = 1234

then the system should get his role from DB and redirect him to his respected home page (student/home)

0 likes
3 replies
jlrdw's avatar

Have a method that verifies the logged in users role matches the required Role.

You should really View Jeffries free lessons own Authentication and authorization. They are included in his laravel 7 from scratch series.

RamjithAp's avatar
Level 10

Way 1: Go to your Auth/logincontroller and use the method

  public function authenticated(Request $request, $user)
      {
          if($user->role=="student") {
            return redirect("/student-home");
          }elseif(){
        //repeat for rest of the roles
    }
      }

Way 2: Go to home controller index method and show the blade view based on role.

  public function index()
      {
          if($user->role=="student") {
            $data = //collect student related data
            return view("student.home",compact("data"));
          }elseif(){
        //repeat for rest of the roles
    }
      }
mware's avatar

Another option for you. In LoginController there is a protected property called $redirectTo that is set to '/home' by default. You can perform some logic in the constructor that checks for a logged in user, and then sets the $redirectTo property according to the logged in user's role.

Please or to participate in this conversation.