Edit: my $id contains an array
@php($ids = array('Fruit','Vegetables')
I just want to display the old input of my checkbox and textarea when the page returns with errors based on my validations. Here is the form elements. I tried using the old(method) but its not working for me.
<form method = "post" action = "tomyController">
<input type = "text" name = "vendor" >
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0">
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1">
<textarea name = "remarks[{{id}}]"></textarea>
@endforeach
</form>
I tried the How to show old data of checkbox in Laravel? but didnt work on mine. Here is how i implemented it.
<form method = "post" action = "tomyController">
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0"
@if(is_array(old('check[$id][0]')) && in_array(0, old('check[$id][0]'))) checked @endif)>
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1"
@if(is_array(old('check[$id][1]')) && in_array(1, old('check[$id][1]'))) checked @endif)>
<textarea name = "remarks[{{id}}]">{{ old('remarks[$id]') }}</textarea>
@endforeach
</form>
My controller code is here.
public function store(Request $request) {
$post = $request->all();
$validator = Validator::make($request->all(), [
"vendor" => "required",
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput($post);
}
else
#my other codes
}
Change your form
to the following-
<form method = "post" action = "tomyController">
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0"
@if(is_array(old('check['.$id.']')) && (0==old('check['.$id.'][0]'))) checked @endif>
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1"
@if(is_array(old('check['.$id.']')) && (1== old('check['.$id.'][1]'))) checked @endif>
<textarea name = "remarks[{{$id}}]">{{ old('remarks['.$id.']') }}</textarea>
@endforeach
</form>