I have form where I can change user role. When I try to change and hit Save button I've got invalid foreach() argument error and previous record in database for this user is deleted completely.
This is my form
{!! Form::model($user, ['method' => 'PATCH','route' => ['admin.addper', $user->user_id]]) !!}
<div class="row">
<div class="col-md-4 col-xs-12">
<div class="form-group">
<label for="title" class="control-block">Username:</label>
</div>
</div><!-- end col-4/12 -->
<div class="col-xs-12 col-md-8">
<div class="form-group">
<label for="title" class="control-block">Choose which Role you want to assign to user:</label><br>
@foreach($roles as $value)
<strong></strong>
<br/>
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
And this is the controller part
public function update(Request $request, $id)
{
$this->validate($request, [
'roles' => 'required'
]);
$input = $request->all();
$user = User::find($id);
$user->update($input);
DB::table('role_user')->where('user_id',$id)->delete();
foreach ($request->input('roles') as $key => $value) {
$user->attachRole($value);
}
return redirect()->route('users')
->with('success','User Role Updated Successfully');
}
The error is on the foreach
in the controller
ErrorException: Invalid argument supplied for foreach()
Since I'm sure that I pass correct user_id
and correct value of role_id
why is this error?
dd($request->input('roles'));
return correct id which I'm choose on checkbox.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire