Christopher Paz
Christopher Paz

Reputation: 31

There is no role in id '#' in Spatie Laravel Jetstream

I am trying to create a Laravel project that uses Jetstream, a full-page Livewire component, and I am trying to use spatie/laravel-permission for the permissions. I always get the error of

Spatie\Permission\Exceptions\RoleDoesNotExist There is no role with id 2.

I have three roles

1=Super Admin 
2=Admin 
3=User

Can anyone help me to solve this problem?

User Livewire Component

use Livewire\Component;
use App\Models\User;
use App\Models\Role;

class Users extends Component
{
    public $userId;
    public $userData;

    public function addRole()
    {
        $this->userData = User::find($this->userId);
        $this->userData->assignRole(2);
    }
}

Upvotes: 1

Views: 2843

Answers (2)

Thamnees
Thamnees

Reputation: 15

I also face the same situation and fixed it later as follows. change the value of 'guard_name' column in roles table and permissions table.

role table image

Upvotes: 0

jalal al
jalal al

Reputation: 88

Clear Spatie cache

php artisan cache:forget spatie.permission.cache 

then

php artisan cache:clear

if not work add this code to model

protected $guard_name = 'api';

Upvotes: 6

Related Questions