I have got 3 tables: users; permissions; user_permissions.
My pivot table (user_permissions) has got 2 columns: user_id and permission_id.
How would I go about removing a permission for a user using the permission name, or an array of permissions?
So for example I could have:
array:1 [
0 => "access_documentation"
]
Or I could have this:
array:4 [
0 => "general_user"
1 => "access_pagespeed"
2 => "access_documentation"
3 => "access_admin"
]
I have got a relationship for the users permissions, which returns the names.
public function permissions()
{
return $this->belongsToMany(Permission::class, 'user_permissions');
}
What I have based on answers provided below:
public function save(User $user, Request $request)
{
$params = Permission::query()->whereIn('name', $request->input('data'));
if ($params->count() === 0) {
return;
}
$params = $params->pluck('id')->toArray();
$user->permissions()->detach();
// $user->permissions()->attach(6);
// $user->permissions
}
Edit
My permissions are split into categories, so when updating them I only send an array of permissions for that category, so doing $user->permissions()->detach() would detach unrelated permissions.
Is there a way I can say only detach or sync all with a certain category ID?
Example user_permissions
id user_id permission_id
1 32 1
Example permissions
id name category_id
1 edit_users 3
** Edit 2 **
$params returns selected permissions within that category, so based on the image below this will be an array [ 'general_user', 'access_pagespeed', 'access_admin' ]. If I was to click the fourth item in the list below then $params would then of course be [ 'general_user', 'access_pagespeed', 'access_admin', 'access_documentation' ].

$params = Permission::query()->whereIn('name', $request->input('data'));is returning all permission on specific category? - Ray A