Support the ongoing development of →
posted 3 years ago
Last updated 2 years ago.

If you do

$name = Auth::user()->roles();

you are accessing the relationship object of the user with roles

if you do

$name = Auth::user()->roles;

You are accessing a collection of Role models that are associated to the user

If you want to get the first role assigned to the user (Assuming is a Has Many) you should do it like this

$role = Auth::user()->roles->first();
if (!$role) {
    // if there is no role assigned do something
$name = $role->name;

Of course it will depend also on what you want to do if there is no role assigned. Hope it helps.


Thank you for your help. This is what I have and now I get Property [name] does not exist on this collection instance.

public function redirectTo() { $role = Auth::user()->roles->first(); if (!$role) { $this->redirectTo = route('user.dashboard'); return $this->redirectTo; } else if(Route::has($name = Auth::user()->roles->name.'.dashboard')){ return route($name); } } }


That is because you are trying to get a property Name out of the collection

$name = Auth::user()->roles->name

Auth::user()->roles returns a Collection. You have to grab the first item in the collection with first like you already did in $role = Auth::user()->roles->first() so just use the $role variable instead of getting the roles again from the user from the guard.


OK I have this and its logging in no matter what role to the /home.blade.php Do I need to make a change in the redirectifauthenticated file as well?

public function redirectTo() { $role = Auth::user()->roles->first(); if (!$role) { $this->redirectTo = route('user.dashboard'); return $this->redirectTo; } else if(Route::has($name =$role.'.dashboard')){ return route($name); dd($name); } } }


yeah, if fact you should have this probably as a helper or something because you may need it in different places, like in the middleware but also in the login controller after a user logs in, registration ETC.


Or you could create a new middleware and apply it to the stack. There are many options but without more context it may need more work.


Doesn't matter what I do, it all goes back to home.blade.php. I want it to go to a certain dashboard based on the role so for instance registrar role goes to the route registrar.dashboard which in the controller links to a view registrar.dashboard. I am really struggling with the redirects.


My other option is to do something like this but I get Route[registrar.dashboard] not found

` Route::get('/dashboard','RegistrarController@dashboard')->name('registrar.dashboard');

public function redirectTo() { if(Auth::user()->hasRole('registrar')){ $this->redirectTo = route('registrar.dashboard'); return $this->redirectTo; } else{ if(Auth::user()->hasRole('webmaster')){ $this->redirectTo = route('webmaster.dashboard'); return $this->redirectTo; } } if(Auth::user()->hasRole('team')){ $this->redirectTo = route('team.dashboard'); return $this->redirectTo; } else { if(Auth::user()->hasRole('sponsor')){ $this->redirectTo = route('sponsor.dashboard'); return $this->redirectTo; } }

if(Auth::user()->hasRole('pastor')){ $this->redirectTo = route('pastor.dashboard'); return $this->redirectTo; } else { if(Auth::user()->hasRole('pilgrim')){ $this->redirectTo = route('pilgrim.dashboard'); return $this->redirectTo; } } `


Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.