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

boyjarv's avatar

RoleDoesNotExist when seeding

Please help, when I run php artisan migrate:refresh --seed I get: Spatie\Permission\Exceptions\RoleDoesNotExist

There is no role with id 1.

at vendor/spatie/laravel-permission/src/Exceptions/RoleDoesNotExist.php:16 12▕ } 13▕ 14▕ public static function withId(int $roleId) 15▕ { ➜ 16▕ return new static("There is no role with id {$roleId}."); 17▕ } 18▕ } 19▕

  +1 vendor frames 

2 database/seeders/PermissionSeeder.php:44 Spatie\Permission\Models\Role::findById()

  +8 vendor frames 

11 database/seeders/DatabaseSeeder.php:22 Illuminate\Database\Seeder::call("Database\Seeders\PermissionSeeder")

Here is my databaseseeder:

public function run()
    {
        $this->call(RoleSeeder::class);
        $this->call(CompanyTableSeeder::class);
        $this->call(ContactTableSeeder::class);
        $this->call(NoteTableSeeder::class);
        $this->call(ArtworkTableSeeder::class);
        $this->call(PermissionSeeder::class);
        $this->call(AdminSeeder::class);
    }

so I am seeding the Roles first so surely they have IDs in :

public function run()
    {
        Role::create(['guard_name' => 'superadmin', 'name' => 'administrator']);
        Role::create(['guard_name' => 'admin', 'name' => 'manager']);
        Role::create(['guard_name' => 'editor', 'name' => 'editor']);
        Role::create(['guard_name' => 'user', 'name' => 'web']);
    }

then my Permissions seeder is:

public function run()
    {
        $permission1 = Permission::create(['name' => 'Create Company']);
        $permission2 = Permission::create(['name' => 'Create Contact']);
        $permission3 = Permission::create(['name' => 'Create Artwork']);
        $permission4 = Permission::create(['name' => 'Create User']);
        $permission5 = Permission::create(['name' => 'Create Item']);
        $permission6 = Permission::create(['name' => 'Create Todo']);
        $permission7 = Permission::create(['name' => 'Edit Company']);
        $permission8 = Permission::create(['name' => 'Edit Contact']);
        $permission9 = Permission::create(['name' => 'Edit Artwork']);
        $permission10 = Permission::create(['name' => 'Edit User']);
        $permission11 = Permission::create(['name' => 'Edit Item']);
        $permission12 = Permission::create(['name' => 'Edit Todo']);
        $permission13 = Permission::create(['name' => 'Delete Company']);
        $permission14 = Permission::create(['name' => 'Delete Contact']);
        $permission15 = Permission::create(['name' => 'Delete Artwork']);
        $permission16 = Permission::create(['name' => 'Delete User']);
        $permission17 = Permission::create(['name' => 'Delete Item']);
        $permission18 = Permission::create(['name' => 'Delete Todo']);
        $permission19 = Permission::create(['name' => 'View Company']);
        $permission20 = Permission::create(['name' => 'View Contact']);
        $permission21 = Permission::create(['name' => 'View Artwork']);
        $permission22 = Permission::create(['name' => 'View User']);
        $permission23 = Permission::create(['name' => 'View Item']);
        $permission24 = Permission::create(['name' => 'View Todo']);

        $superadminrole = Role::findById(1);
        $adminrole = Role::findById(2);
        $editorrole = Role::findById(3);
        $webrole = Role::findById(4);
        $superadminrole->givePermissionTo(
            [
                $permission1,
                $permission2,
                $permission3,
                $permission4,
                $permission5,
                $permission6,
                $permission7,
                $permission8,
                $permission9,
                $permission10,
                $permission11,
                $permission12,
                $permission13,
                $permission14,
                $permission15,
                $permission16,
                $permission17,
                $permission18,
                $permission19,
                $permission20,
                $permission21,
                $permission22,
                $permission23,
                $permission24,
            ]
        );
        $adminrole->givePermissionTo(
            [
                $permission1,
                $permission2,
                $permission3,
                $permission4,
                $permission5,
                $permission6,
                $permission7,
                $permission8,
                $permission9,
                $permission10,
                $permission11,
                $permission12,
                $permission18,
                $permission19,
                $permission20,
                $permission21,
                $permission22,
                $permission23,
                $permission24,
            ]
        );
        $editorrole->givePermissionTo(
            [
                $permission7,
                $permission8,
                $permission9,
                $permission10,
                $permission11,
                $permission12,
                $permission19,
                $permission20,
                $permission21,
                $permission22,
                $permission23,
                $permission24,
            ]
        );
        $webrole->givePermissionTo(
            [
                $permission19,
                $permission20,
                $permission21,
                $permission22,
                $permission23,
                $permission24,
            ]
        );
    }
0 likes
12 replies
Sinnbeck's avatar

Never hardcode ids

$superadminrole = Role::findByName('administrator');
boyjarv's avatar

@Sinnbeck Thanks but as expected, now I get:

There is no role named administrator.

at vendor/spatie/laravel-permission/src/Exceptions/RoleDoesNotExist.php:11 7▕ class RoleDoesNotExist extends InvalidArgumentException 8▕ { 9▕ public static function named(string $roleName) 10▕ { ➜ 11▕ return new static("There is no role named {$roleName}."); 12▕ } 13▕ 14▕ public static function withId(int $roleId) 15▕ {

  +1 vendor frames 

2 database/seeders/PermissionSeeder.php:44 Spatie\Permission\Models\Role::findByName("administrator")

  +8 vendor frames 

11 database/seeders/DatabaseSeeder.php:22 Illuminate\Database\Seeder::call("Database\Seeders\PermissionSeeder")

Sinnbeck's avatar

@boyjarv That is not expected. There must be something we arent seeing. I cannot recreate the error.

To make sure it exists you can do

$superadminrole = Role::findOrCreate('administrator', 'superadmin');
boyjarv's avatar

@Sinnbeck thanks but now I get:

Spatie\Permission\Exceptions\GuardDoesNotMatch

The given role or permission should use guard superadmin instead of api.

at vendor/spatie/laravel-permission/src/Exceptions/GuardDoesNotMatch.php:12 8▕ class GuardDoesNotMatch extends InvalidArgumentException 9▕ { 10▕ public static function create(string $givenGuard, Collection $expectedGuards) 11▕ { ➜ 12▕ return new static("The given role or permission should use guard {$expectedGuards->implode(', ')} instead of {$givenGuard}."); 13▕ } 14▕ }

Sinnbeck's avatar

@boyjarv Why are you setting different guards for all of them? Is your app already set up to use different guards for different things or ?

jaseofspades88's avatar

If Spatie roles and permissions is saying the role doesn't exist, it's because the role is not in your database when trying to assign it. Perhaps you're doing something in an accessor or before your seeders are running and that's where the role doesn't yet exist?

Sinnbeck's avatar

@boyjarv Any chance that any of the many other seeders does something? Maybe move it up so its right after RoleSeeder

boyjarv's avatar

nope, that didn't work, still getting: Spatie\Permission\Exceptions\RoleDoesNotExist

There is no role named administrator.

at vendor/spatie/laravel-permission/src/Exceptions/RoleDoesNotExist.php:11 7▕ class RoleDoesNotExist extends InvalidArgumentException 8▕ { 9▕ public static function named(string $roleName) 10▕ { ➜ 11▕ return new static("There is no role named {$roleName}."); 12▕ } 13▕ 14▕ public static function withId(int $roleId) 15▕ {

  +1 vendor frames 

2 database/seeders/PermissionSeeder.php:44 Spatie\Permission\Models\Role::findByName("administrator")

  +8 vendor frames 

11 database/seeders/DatabaseSeeder.php:18 Illuminate\Database\Seeder::call("Database\Seeders\PermissionSeeder")

da_Mask's avatar

I get this same issue. When I do this in my DatabaseSeeder

 $this->call(RolesAndPermissionsSeeder::class);
  dump(DB::table('roles')->get());
  $superAdmin->assignRole('super-admin');

I get this output in the terminal

Illuminate\Support\Collection^ {#2689 // database/seeders/DatabaseSeeder.php:24
  #items: array:2 [
    0 => {#2894
      +"id": 1
      +"team_id": 2
      +"name": "marketing-manager"
      +"guard_name": "web"
      +"created_at": "2023-01-31 00:01:15"
      +"updated_at": "2023-01-31 00:01:15"
    }
    1 => {#2878
      +"id": 2
      +"team_id": 1
      +"name": "super-admin"
      +"guard_name": "web"
      +"created_at": "2023-01-31 00:01:15"
      +"updated_at": "2023-01-31 00:01:15"
    }
  ]
  #escapeWhenCastingToString: false
}
   Spatie\Permission\Exceptions\RoleDoesNotExist 
  There is no role named `super-admin`.
ranto's avatar

Hi, it will be happening because you are setting multiple guards, so when the seeders try to assign roles or permissions. It runs on the default guard.

By default, the spatie package uses the first guard defined in the config/auth.php file. But you can specify the guard tho use on the User model.

protected function guard()
{
    return Auth::guard('guard-name');
}

Opinion: I think you are using multiples guards as múltiples roles. I always think of guards to be use when you have multiple users models/tables (or multiple ways to login an user)

Link to spatie docs about multiple guards https://spatie.be/docs/laravel-permission/v5/basic-usage/multiple-guards#content-the-downside-to-multiple-guards

uQualify's avatar

@ranto - Thanks Ranro. We had a dynamic guard name and this answer led me to the root of the issue.

1 like

Please or to participate in this conversation.