danichangt's avatar

hasRoles() undefined in auth.

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.

1 like
8 replies
vincent15000's avatar

It should work.

Are you sure that you have a connected user when you are trying auth()->user()->hasRole('role'); ?

1 like
danichangt's avatar

@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.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

dump user in both cases?

2 likes
Dhruv29102003's avatar

@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);
}
1 like
afraan's avatar

$user = User::findOrFail(Auth::user()->id);

1 like
Dhruv29102003's avatar

@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);
}
1 like

Please or to participate in this conversation.