First things first, your input name will need to be roles[] so that it can accept multiple values.
At that point, roles will be an array of role IDs, so you should be able to simply do:
$user->roles()->sync(Input::get('roles'));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a simple form for creating a new user which includes a checkbox list of all the available roles. I'm trying to figure out how to iterate through the roles once the user has been created and then save the roles to my roles table.
public function store(Request $request)
{
$rules = array(
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:10'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails())
{
return Redirect::to('admin/users/create')
->withInput();
}else{
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password');
$user->save();
// How to get the roles
// Then iterate over them to save them to my user_roles table.
Session::flash('success', 'Your new user has been added.');
return Redirect::to('admin/users');
}
}
Checkboxes look like
@foreach($roles as $role)
<div class="checkbox">
<label>
<input type="checkbox" name="roles" id="role_{{ $role->name }}" value="{{ $role->id }}" /> {{ $role->display_name }}
</label>
</div>
@endforeach
First things first, your input name will need to be roles[] so that it can accept multiple values.
At that point, roles will be an array of role IDs, so you should be able to simply do:
$user->roles()->sync(Input::get('roles'));
Please or to participate in this conversation.