How can I edit more than one radio button in a loop in laravel
<?php $i = 1; ?>
@foreach ($products as $product)
<tr>
<td scope="row">{{ $i }}</td>
<td>{{ $product->name ?? '' }}</td>
<td><img src="{{asset($product->image)}}" alt="product img"></td>
<td>
<div >
<div >
<input type="hidden" name="id" value="{{$product->id}}">
<input type="radio" name="status[{{$product->id}}]" value="active" {{($product->status == 'active') ? 'checked' : '' }} id="Active">
<label for="Active">
active
</label>
</div>
<div >
<input type="radio" name="status[{{$product->id}}]" value="deactive" {{ ($product->status == 'deactive') ? 'checked' : '' }} id="Deactive" >
<label for="Deactive">
deactive
</label>
</div>
</div>
</td>
</tr>
@endforeach
public function active_deactive(Request $request){
if (isset($request->status)) {
foreach ($request->status as $status_id) {
$product = Product::where('id', $request->id)
->update(['status' => $request->status]);
}
}
return Redirect()->back();
}
How can I modify more than one radio button as shown in the picture: enter image description here
CodePudding user response:
$request->id
is going to be the last id
, since you've got multiple <input name="id">
elements. You need to send them as an array:
<input name="id[]" value="{{ $product->id }}">
Then reference them on the backend:
foreach ($request->input('status') as $key => $status) {
Product::where('id', $request->input('id')[$key])->update(['status' => $status]);
}
Or, since you already have name="status[{{ $product->id }}]"
, you can just use that instead:
foreach ($request->input('status') as $productId => $status) {
Product::where('id', $productId)->update(['status' => $status]);
}