Assign a new role to Entrust

As part of my approval process, I want the administrator to assign different roles to users on request.

I want to "separate" the current user role and add a new one.

$id = Input::get('submit');
$user = User::where('id','=', $id)->first();
$user->attachRole(2);

$user->save();

The above code gets the ID of the user for whom I want to reassign the role, and then attachRole, assigned a new one, but I can’t delete the previous one.

However, it does not delete the previous role of the user to whom it was assigned.

$user->detachRole(USER ID HERE);

doesn't seem to work.

The only problem is that if it were in the Role table, I could use $ user-> role () → delete (something like this) where the user assignment for roles is in 'assign_roles.

Any help would be greatly appreciated.

+4
1

, , :

 /**
 * Save roles inputted from multiselect
 * @param $inputRoles
 */
public function saveRoles($inputRoles)
{
    if(! empty($inputRoles)) {
        $this->roles()->sync($inputRoles);
    } else {
        $this->roles()->detach();
    }
}
+10

Source: https://habr.com/ru/post/1534304/


All Articles