Using laravels Spatie/Roles is there a way to get the current users role who is logged in?
I've tried
if($this->hasRole('admin')) { //do something }
but it wont work
In your Model that uses the Spatie\Permission\Traits\HasRoles
trait, which is in most cases User
, you can call various methods for verifying or retrieving roles.
They are:
roles
hasRole
hasAnyRole
hasAllRoles
getRoleNames
For a given user,
$roles = $user->getRoleNames();// Returns a collection
Reference here.
{{ Auth::user()->roles->pluck('name') }}
Figured it out, for those of you out there this is how you do it.
if(Auth::user()->hasRole('my_role_name'))
{
// do something
}
//otherwise do something else
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With