As part of my 'approval' process, I want the admin to allocate users different roles, on demand.
I want to 'detach' the user's current role and add a new one.
$id = Input::get('submit');
$user = User::where('id','=', $id)->first();
$user->attachRole(2);
$user->save();
The code above gets the user's ID that I want to reassign a role to, and then the attachRole, assigned the new one, but I can't delete the previous one.
However, it doesn't remove the user's previous role it was assigned to.
$user->detachRole(USER ID HERE);
doesn't seem to work.
The only problem is, if it was in the 'Role' table, I could possibly use $user->roles()->delete (something like that), where has the assignment of users to roles is in the 'assigned_roles' table.
Any help would be hugely appreciated.
Here is an example function which you could use to save your roles, it accepts an array of role id's:
/**
* Save roles inputted from multiselect
* @param $inputRoles
*/
public function saveRoles($inputRoles)
{
if(! empty($inputRoles)) {
$this->roles()->sync($inputRoles);
} else {
$this->roles()->detach();
}
}