Support the ongoing development of Laravel.io →
posted 1 year ago
Last updated 4 months ago.
0

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.

0

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); } } }

0

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.

0

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); } } }

0

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.

0

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.

0

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.

0

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

` Route::get('/dashboard','[email protected]')->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; } } `

0

Sign in to participate in this thread!

Cloudways

Your banner here too?

Moderators

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.

© 2022 Laravel.io - All rights reserved.