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

marcoplus's avatar

different login via role direct to specific dashboard

I need to limit access to a role I created and redirect after login to a specific page, I use spatie for permissions and roles, reading online I found that I had to insert this code to redirect to a specific page but it doesn't work, and I inserted this in the sidebar and it doesn't work either,

LoginController

 protected function authenticated(Request $request, $user)
    {
        if ($user->hasRole('customer')) {
            return redirect()->route('customer-dashboard');
        } elseif ($user->hasRole('admin')) {
            return redirect()->route('dashboard');
        } elseif ($user->hasRole('operator')) {
            return redirect()->route('dashboard');
        } elseif ($user->hasRole('office')) {
            return redirect()->route('dashboard');
        }  elseif ($user->hasRole('manager')) {
            return redirect()->route('dashboard');
        } else {
            return redirect('/dashboard');
        }
    }

sidebar.blade.php

@can('permission')

@can('customer')

@andcan

@andcan


0 likes
8 replies
marcoplus's avatar

@fabyo I also tried to do this, but it doesn't work

@if(auth()->user()->can('permission'))

		@if(auth()->user()->can('customer'))
		 @endif
@endif
fabyo's avatar

@marcoplus If the roles and permissions already exist, it might not be detecting them due to caching.

php artisan cache:clear
php artisan config:clear
php artisan route:clear
marcoplus's avatar

@fabyo yes i usually run this command php artisan optimize:clear, but i also ran the single commands but i still see the menus with the role and permission i created that i wanted to limit and hide and deny access, it still doesnt work

fabyo's avatar

@marcoplus Have you added the HasRoles trait to the relevant models

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}
```
Snapey's avatar

@can is a permissions check. Do you really have a permission called 'permission'?

@andcan should be @endcan

Redirection depends on what frontend framework you used and what version of Laravel

marcoplus's avatar

@Snapey yes to try after I failed to do with the permissions I had created, I created a permission called like this and assigned it to all roles except one, but nothing I don't understand why it doesn't work, the version of laravel I'm using is "laravel/framework": "^10.10"

Please or to participate in this conversation.