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

FareedR's avatar

How can I sync new permission for specific roles

How can I update new permission for admin role ? Currently admin can edit post . how can I make admin can delete post ? I'm using spatie laravel permissions. Anyone ? I'm aware with this function but didnt worked for me . should pluck ?

$user->syncPermissions() 
0 likes
9 replies
Sinnbeck's avatar

Are you trying to change permissions of an admin role or change the permissions of that specific user?

Sti3bas's avatar

@fareedr if you're using permission middleware, you have to give delete permission to your admin role.

For example if permission is called delete post, then you should: Role::findByName('admin')->givePermissionTo('delete post');

Otherwise you should give it for the user: $user->givePermissionTo('delete post');

FareedR's avatar

trying to change permission of an admin role . i tried this method . still no luck . im trying to give an access for admin role to delete post


$role->syncPermissions($request->get('permissions');

Sinnbeck's avatar

Should it delete all other permissions? Sync does. Look at the example by @sti3bas to add a permission to the role, without removing the others. (unless of course the $request->get('permissions') contains all needed permissions)

FareedR's avatar

it worked now . i need to change role not specific user . i tried syncPermissions and somehow it worked . syncPermission didnt delete other permissions . thank you for your quick response

Sinnbeck's avatar

Like I said. If your incoming request contains all needed permissions, then sync is the right tool

FareedR's avatar

btw , i try to change role from user to admin . unfortunately, spatie delete the data and didnt assign a new role . just assumed that role_id 3 is admin

$role_id = "3";
role = Role::where('id',$role_id)->first();
        $user->syncRoles($role);
Sinnbeck's avatar

Check your database for the right is. Or even better, find the name you gave it

Please or to participate in this conversation.