It should work.
Are you sure that you have a connected user when you are trying auth()->user()->hasRole('role'); ?
I want to check what type of user have been logged in. I'm using Spatie Laravel Permission to add roles and permissions to a user. I read the Spatie docs but can't found something.
I'm trying to check the role of an auth user but when I use
auth()->user()->hasRole('role');
I have an error saying the method 'hasRole' is undefined. If I use
$user = $request->user();
$user->hasRole('role');
that's work fine.
In my User model I added use Spatie\Permission\Traits\HasRoles; and use HasRoles;
Thank you a lot.
It should work.
Are you sure that you have a connected user when you are trying auth()->user()->hasRole('role'); ?
@vincent15000 Yes, the user is connected. I use dump() and can fix the problem. I don't know why Intelephense VSCode mark the method like undefined.
dump user in both cases?
@Snapey Thank you, that helped me.
@danichangt what did you find? Anything that could help others in the future?
@Snapey I am facing the same error but unable to resolve tried the same thing as well below is the code which is giving the error Can you please help me to resolve
$user = $this->userRepository->findWithoutFail(auth()->id());
unset($user->password);
$customFields = false;
$bannedRoles = $user->getRoleNames()->map(function ($element) {
return config('permission.banned_roles')[$element];
})->toArray();
if (auth()->user()->hasRole('institution')) {
array_push($bannedRoles[0], "driver", "dispatcher", "client", "manager", "employee");
}
$bannedRoles = array_merge(...$bannedRoles);
$role = $this->roleRepository->whereNotIn('name', $bannedRoles)->pluck('name', 'name');
if (auth()->user()->hasRole('admin') || auth()->user()->hasRole('Super Admin')) {
$townHall = $this->townHallRepository->pluck('name', 'id');
} else {
$townHall = $this->townHallRepository->myMarkets()->pluck('name', 'id');
}
$rolesSelected = $user->getRoleNames()->toArray();
$customFieldsValues = $user->customFieldsValues()->with('customField')->get();
//dd($customFieldsValues);
$hasCustomField = in_array($this->userRepository->model(), setting('custom_field_models', []));
if ($hasCustomField) {
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->userRepository->model());
$customFields = generateCustomField($customFields, $customFieldsValues);
}
$townHall = $townHall->toArray();
$townHall[null] = null;
$riderTypes = $this->riderTypesRepository->getRiderTypes()->pluck('name', 'id');
return view('settings.users.profile')
->with("user", $user)
->with("role", $role)
->with('townHall', $townHall)
->with("customFields", isset($html) ? $html : false)
->with("rolesSelected", $rolesSelected)
->with('riderTypes', $riderTypes)
->with('riderIdSelected', null);
}
$user = User::findOrFail(Auth::user()->id);
@afraan if you are able to help then please help me with that it can be great
$user = $this->userRepository->findWithoutFail(auth()->id());
unset($user->password);
$customFields = false;
$bannedRoles = $user->getRoleNames()->map(function ($element) {
return config('permission.banned_roles')[$element];
})->toArray();
if (auth()->user()->hasRole('institution')) {
array_push($bannedRoles[0], "driver", "dispatcher", "client", "manager", "employee");
}
$bannedRoles = array_merge(...$bannedRoles);
$role = $this->roleRepository->whereNotIn('name', $bannedRoles)->pluck('name', 'name');
if (auth()->user()->hasRole('admin') || auth()->user()->hasRole('Super Admin')) {
$townHall = $this->townHallRepository->pluck('name', 'id');
} else {
$townHall = $this->townHallRepository->myMarkets()->pluck('name', 'id');
}
$rolesSelected = $user->getRoleNames()->toArray();
$customFieldsValues = $user->customFieldsValues()->with('customField')->get();
//dd($customFieldsValues);
$hasCustomField = in_array($this->userRepository->model(), setting('custom_field_models', []));
if ($hasCustomField) {
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->userRepository->model());
$customFields = generateCustomField($customFields, $customFieldsValues);
}
$townHall = $townHall->toArray();
$townHall[null] = null;
$riderTypes = $this->riderTypesRepository->getRiderTypes()->pluck('name', 'id');
return view('settings.users.profile')
->with("user", $user)
->with("role", $role)
->with('townHall', $townHall)
->with("customFields", isset($html) ? $html : false)
->with("rolesSelected", $rolesSelected)
->with('riderTypes', $riderTypes)
->with('riderIdSelected', null);
}
Please or to participate in this conversation.