Assign multiple roles by id
How do I assign multiple roles by id? I have an array of role id's that I need to assign to a user.
This doesn't seem to work ..
$user->roles()->attach(Role::whereIn('id', $data['roles']));
It's not working because you do not have an array yet. You need to finish the query:
$user->roles()->attach(Role::whereIn('id', $data['roles'])->pluck('id')->toArray());
But if you already have the role ids, why wouldn't you just do this:
$user->roles->attach($data['roles']);
Wanted to verify the input with the existing roles. Your suggestions worked - thanks
@mdk999 - could you please mark @wilk_randall answer as the solution. It helps others if they have the same problem
Please or to participate in this conversation.