Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MHasanKN's avatar

App\Models\User::getStoredRole(): Return value must be of type Spatie\Permission\Contracts\Role, App\Models\Company returned

I might be confused or the document doesn't explain well but here is the problem: I had set up Laratrust but it wasn't very good with my current config. Hence, while browsing I saw Spatie had this feature but I had never used it before. I removed all code for Laratrust. Now after setting up everything for Spatie when I am assigning a role I get the this error:

App\Models\User::getStoredRole(): Return value must be of type Spatie\Permission\Contracts\Role, App\Models\Company returned

The error is here:

if ($request->account_type === 'company') {
                $company = Company::create([
                    'user_id' => $user->id,
                    'company_name' => $request->name,
                ]);
                session(['selected_company' => $company->id]);
                $user->assignRole('Super Admin', $company);
            }

Inside config.php I set the teams option to true and teams_foreign_key to 'company_id'.

Note that I have only made global roles in seeder which means these roles have company_id as null. This means no specific role for a company exists. I am not willing to make same roles for each company because for my current setup we don't need different roles for each company. All the global roles will be applied/needed. Users have oneToMany realtionship with companies where one user can be a part of a lot of companies and will have different roles in them. Somewhere they can be an admin and somewhere they can be someone else. I have not changed the Company model only User model has this use HasRoles

0 likes
3 replies
tykus's avatar

Nothing is returned by the code you shared so it is impossible to understand the error message in context; can you provide all of the relevant code?

MHasanKN's avatar

@tykus sorry I did not clarify the error happens on this line:

$user->assignRole('Super Admin', $company);

It does not return anything but the function assignRole calls a bunch of other functions and these internal calls do return eachother something.

Below is the function that returns something and is giving me error

vendor\spatie\laravel-permission\src\Traits\HasRoles.php

protected function getStoredRole($role): Role
    {
        if ($role instanceof \BackedEnum) {
            $role = $role->value;
        }

        if (is_int($role) || PermissionRegistrar::isUid($role)) {
            return $this->getRoleClass()::findById($role, $this->getDefaultGuardName());
        }

        if (is_string($role)) {
            return $this->getRoleClass()::findByName($role, $this->getDefaultGuardName());
        }

        return $role;
    }
tykus's avatar

@MHasanKN it looks like maybe you have mis-configured the Spatie Laravel Permissions package; what is the value assigned to

config('permission.models.role');

Please or to participate in this conversation.