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

archiebango's avatar

Multiple Roles (`Admin`, `Members`) and redirect and authenticate

Hi,

I have a multiple role like admin, and member, and I have column in database name role. If the role is admin, it will redirect to /admin page in which members cant access.

What is the best practice for it? and some help to. I tried to check the answers in google but some are not working or outdated (laravel 5.5).

Thanks

0 likes
1 reply
sherwinmdev's avatar

@archiebango You can use middleware for this.

public function handle($request, Closure $next)
{
  $user_role = User::with(‘role’)->find(Auth::id());

  if ($user->role->name == ‘admin’)
  {
    return redirect()->to(‘/admin’);
  }

  return redirect()->to(‘/‘);
}

You can create a helper class and put it there. It all depends on how and when you want to trigger it.

The code above assumes you have the following relationship

  • a user belongs to a role
  • a role has many users

If you use middleware, you can call it at a controller’s construct(). If you use a helper, you can use it let’s say right after a successful login. There are many ways to implement what you’re trying to do. Hope this gives you an idea.

1 like

Please or to participate in this conversation.