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

moe1047's avatar

Roles in laravel 5

can sumone please help me with redirecting based on roles i have two roles (admin,user) i want to redirect User to USER_HOME and admin to ADMIN_HOME ,and of course i have role column in the users table pleease help

0 likes
4 replies
pedroroccon's avatar

You're using Entrust? If not, I strongly recommend this package to your projects. You can install via composer. Link: https://github.com/Zizaco/entrust

With Entrust installed, you can try this:

public function index()
{
    if(Entrust::hasRole('admin'):
        return redirect('admin/home');
    elseif(Entrust::hasRole('user'):
        return redirect('user/home');
    else:
        // Do another thing.
    endif;
}
mstnorris's avatar

@moe1047 are you using a controller?

public function home()
{
    if ( auth()->user()->role == 'admin' ) {
        return view('admin_view');
    } else if ( auth()->user()->role == 'user' ) {
        return view('user_view');
    } else {
        return "you don't have a role!"; // just an example
    }
}
jlrdw's avatar

One of the better answers on this site, I wish closures didn't exist and folks had to use controllers. Would make things easier.

ayekoto's avatar

i wish i can help you but not something i can explain online. what is the best way we can share code online?

Please or to participate in this conversation.