i'm trying to attach role for user using select box but it doesn't work
i've Roles table and bridge table between Roles and Users Table
i know it's many-to-many relationship and i should use check boxes instead of select box
but at the time for some reason i need to assign only 1 Role for user
So, i have to use select box
Any ideas?
Controller
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
));
$password = Hash::make($request->password);
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $password;
$user->save();
if ($request['user']) {
$user->roles()->attach(Role::where('name','user')->first());
}
if ($request['editor']) {
$user->roles()->attach(Role::where('name','editor')->first());
}
if ($request['admin']) {
$user->roles()->attach(Role::where('name','admin')->first());
}
return redirect('users')->with('success', 'user is successfully saved');
}
View
<div class="form-group">
<label class="col-12">Roles</label>
<div class="col-12">
<div class="custom-control custom-radio mb-5">
<input class="custom-control-input" type="radio" name=admin" id="example-radio1" value="admin">
<label class="custom-control-label" for="example-radio1">Admin</label>
</div>
<div class="custom-control custom-radio mb-5">
<input class="custom-control-input" type="radio" name=pharmacy" id="example-radio2" value="editor">
<label class="custom-control-label" for="example-radio2">Editor</label>
</div>
<div class="custom-control custom-radio mb-5">
<input class="custom-control-input" type="radio" name=company" id="example-radio3" value="user">
<label class="custom-control-label" for="example-radio3">User</label>
</div>
</div>
</div>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire