Laravel-permission: Can we update or sync roles and permissions?

Created on 31 Jul 2016  路  3Comments  路  Source: spatie/laravel-permission

I have tried to update permissions on role, but with no luck. I get "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array" exception. Is this possible after creating role and assigning permissions to it?

Most helpful comment

Roles and permissions have a plain Eloquent many-to-many relationship, so you should be able to use the sync method:

$role->permissions()->sync([$permissionA, $permissionB]);

All 3 comments

Hmm, the parameter mismatch error mostly occurs when arrays get passed in somewhere instead of strings, or some attribute isn't being casted correctly. Could you post some code snippets?

Thanks for prompt response. I don't have snippets, I had to deliver some demo fast, so I ended up looping through all permissions on some role, revoking them, and then looping again through selected permissions (returned from form, via checkboxes) and assigning them again. This looks like a really bad approach, so my question is how do you sync permissions on some role after they have been created? I don't see that part in the docs.

Roles and permissions have a plain Eloquent many-to-many relationship, so you should be able to use the sync method:

$role->permissions()->sync([$permissionA, $permissionB]);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

notflip picture notflip  路  3Comments

devingray picture devingray  路  3Comments

younus93 picture younus93  路  4Comments

wreighsantos picture wreighsantos  路  4Comments

Dreambox13 picture Dreambox13  路  3Comments