Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I edit more than one radio button in a loop in laravel

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)}}" class="product-img-2" alt="product img"></td>
                                    <td>
                                  <div class="col-md-2">
                                        <div class="form-check form-check-inline">
                                            <input type="hidden" name="id" value="{{$product->id}}">
                                            <input class="form-check-input" type="radio" name="status[{{$product->id}}]" value="active" {{($product->status  == 'active') ? 'checked' : '' }} id="Active">
                                            <label class="form-check-label" for="Active">
                                                active
                                            </label>
                                        </div>
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="status[{{$product->id}}]" value="deactive" {{ ($product->status == 'deactive') ? 'checked' : '' }} id="Deactive" >
                                            <label class="form-check-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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

$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]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading