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

radhamadhavamhostel's avatar

syncPermissions In Spatie showing error

I am trying to seed Admin data into Db. I am assigning 'admin' role to user created and adding permissions to that 'role'.

I am using UUID in this project instead of id

When Seeding showing error

There is no permission named `f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd` for guard `web`. at vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php:11
  7▕ class PermissionDoesNotExist extends InvalidArgumentException

  8▕ {

  9▕     public static function create(string $permissionName, string $guardName = '')

 10▕     {

➜ 11▕ return new static("There is no permission named {$permissionName} for guard {$guardName}.");

 12▕     }

 13▕
 14▕     public static function withId(int $permissionId, string $guardName = '')

 15▕     {

  7 vendor frames

8 database/seeders/CreateAdminUserSeeder.php:33 Spatie\Permission\Models\Role::syncPermissions() +22 vendor frames

31 artisan:35 Illuminate\Foundation\Console\Kernel::handle()

My code

    {
        $user = User::create([
            'fname' => 'Super', 
            'lname' => 'Admin', 
            'email' => '[email protected]',
            'username' => 'Superadmin',
            'password' => Hash::make('mypassword'),
            'user_type' => 'admin'
        ]);
    
        $role = Role::create(['name' => 'admin']);
     
        $permissions = Permission::pluck('id', 'id')->all(); 
   
        $role->syncPermissions($permissions);
     
        $user->assignRole([$role->id]);
    }
0 likes
11 replies
Armani's avatar

I never used Spatie Laravel Permission package but when you use pluck function you don't need to chain all:

$permissions = Permission::pluck('id'); 

maybe you need to convert collection to array, if it needed do it like this:

$permissions = Permission::pluck('id')->toArray(); 
radhamadhavamhostel's avatar

@Armani Still showing

There is no permission named f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd for guard web.
Instead of Spatie which method your are using for manage roles and permissions

Armani's avatar

@radhamadhavamhostel I use mine, it is easy to implement same thing but this package is popular and very good. I think you have to debug your code before judging this package. for example use dd() function to see you get the right data from server:

$user = User::create([
            'fname' => 'Super', 
            'lname' => 'Admin', 
            'email' => '[email protected]',
            'username' => 'Superadmin',
            'password' => Hash::make('mypassword'),
            'user_type' => 'admin'
        ]);
    
        $role = Role::create(['name' => 'admin']);
     
        $permissions = Permission::pluck('id'); 

		dd($permissions);
   
        $role->syncPermissions($permissions);
     
        $user->assignRole([$role->id]);

and check it syncPermissions method needs collection or array as parameter. I think you can do it like this:

$permissions = Permission::pluck('name'); 

or this:

$permissions = Permission::pluck('name')->toArray(); 
radhamadhavamhostel's avatar

@Armani Yes permission id are getting

  0 => "f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd"
  1 => "6dfc266d-bdfa-4955-8a14-6eaed784e6a0"
  2 => "d9ae80a1-25af-4fae-bfbe-511148f69d9b"
  3 => "6479aef2-aeee-4e06-bd0a-cae32530ef79"
  4 => "d6607835-9ab5-46fe-9e1a-fa621d8e4a63"
  5 => "aa4c7532-607b-4e87-bda3-801328ecaea5"
  6 => "e9e6be77-00d8-483a-9745-19e986567465"
  7 => "d89048a9-f025-4a87-b4ff-80fe85caa176"]

Should we select name along with id

Armani's avatar

@radhamadhavamhostel Check this like

$all_roles_in_database = Role::all()->pluck('name');

you can use it like this:

$permissions = Permission::all()->pluck('name'); 

you need the permission names not id

radhamadhavamhostel's avatar

@Armani No need to select roles , This is a seeder and we are creating a new role named 'admin' here and it is inserting into Db

$role = Role::create(['name' => 'admin']);
     
        $permissions = DB::table('permissions')->pluck('id')->toArray(); dd($permissions);
   
        $role->syncPermissions($permissions);
     
        $user->assignRole([$role->id]);
Armani's avatar

@radhamadhavamhostel it was an example just use it like this:

$user = User::create([
            'fname' => 'Super', 
            'lname' => 'Admin', 
            'email' => '[email protected]',
            'username' => 'Superadmin',
            'password' => Hash::make('mypassword'),
            'user_type' => 'admin'
        ]);
    
        $role = Role::create(['name' => 'admin']);
     
        $permissions = Permission::pluck('name'); 
   
        $role->syncPermissions($permissions);
     
        $user->assignRole([$role->id]);
radhamadhavamhostel's avatar

@Armani When Pluck name instead of id. getting error

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "role_id" of relation "role_has_permissions" violates not-null constraint DETAIL: Failing row contains (f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd, null). (Connection: pgsql, SQL: insert into "role_has_permissions" ("permission_id", "role_id") values (f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd, ?))
Table: role_has_permissions
Column	                Type
permission_id	uuid	
role_id	                  uuid ```
radhamadhavamhostel's avatar

@Armani This is what I have followed. Table : roles_has_perm,ission have only 2 fileds.

Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
       $table->uuid('permission_id');
       $table->uuid('role_id');

Above one is from documentation.

When we pluck 'name' from permissions role_is is missing.

null value in column "role_id" of relation "role_has_permissions" violates not-null constraint DETAIL: Failing row contains (f6bc4ae5-f08c-4c9c-8d2b-c7ced151a1dd, null).
Armani's avatar

@radhamadhavamhostel do you know how to use tinker? if you know try to use that to get a better understanding what is going on. run your code line by line to see the issue.

$user = User::create([
            'fname' => 'Super', 
            'lname' => 'Admin', 
            'email' => '[email protected]',
            'username' => 'Superadmin',
            'password' => Hash::make('mypassword'),
            'user_type' => 'admin'
        ]);
    
        $role = Role::create(['name' => 'admin']);
     
        $permissions = Permission::pluck('id'); 
   
        $role->syncPermissions($permissions);
     
        $user->assignRole([$role->id]);
1 like

Please or to participate in this conversation.