Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

472
Views
Multi user role in laravel 8

I want to redirect to the user's dashboard according to role. I have created a separate table for the user role and a separate table for the user. I don't understand how I can do that.

This is my middleware code

public function handle(Request $request, Closure $next)
    {
        if(!Auth::check()){
            return redirect()->route('login.user')->with('error', 'Please login first');
        }

        if(Auth::user()->role == 1){
            return $next($request);
        }

        if(Auth::user()->role == 2){
            return redirect()->route('user.dashboard');
        }
    }

This is my user role table

id user_type
1 admin
2 user

This is my user table

id user_name user_role_id
1 admin 1
2 user 2

I would be very happy if you could help me.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In the User Model you have to define the role relationship.

Put this to your User Model:

    public function role()
    {
       return $this->belongsTo(Role::class, 'user_role_id');
    }

Of course you have to have the Role model, but I guess you already have that

public function handle(Request $request, Closure $next)
    {
        if(!Auth::check()){
            return redirect()->route('login.user')->with('error', 'Please login first');
        }

        if(Auth::user()->role->user_type == 'admin'){
            return $next($request);
        }

        if(Auth::user()->role->user_type == 'user'){
            return redirect()->route('user.dashboard');
        }
    }

Another solution could be without relationship just like this:

public function handle(Request $request, Closure $next)
    {
        if(!Auth::check()){
            return redirect()->route('login.user')->with('error', 'Please login first');
        }

        if(Auth::user()->user_role_id == 1){
            return $next($request);
        }

        if(Auth::user()->user_role_id == 2){
            return redirect()->route('user.dashboard');
        }
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!