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

hetaljoshi's avatar

Laravel Login Issue

I have added multiple roles for users with new table as roles and user role id in user table. Then added as checkrole middleware but once you logged in as admin (role id 1) then it will logged in success fully.

But on next you try to logged in as user (role id 2) it will redirect to login page again.

0 likes
3 replies
hetaljoshi's avatar

Yes I can show you. Below is the code public function handle(Request $request, Closure $next, ...$roles) { $user = Auth::user(); if($user == null){ return redirect()->to('/'); }

    if(in_array(Auth::user()->role->name, $roles))
    {
        return $next($request); 
    } else{
        Session::flash('error', "Permission Denied.");
        return redirect()->back()->withInput();
    }

    return $next($request);
}
Nakov's avatar

Do you get to this in_array(Auth::user()->role->name, $roles) check at all?

Can you do dd(in_array(Auth::user()->role->name, $roles)); before the check and see what is the result?

Please or to participate in this conversation.