I have a product attribute table, in that, I have 2 text fields. So when I enter the values in all text field box. And when I press the update button. the first input row is updated successfully but the others not. Here is the picture of the blade file:
Here is my code Blade.php
<form name="editAttributeForm" id="editAttributeForm" method="post" action="{{url('admin/edit-attributes/'.$productdata['id'])}}" enctype="multipart/form-data"> @csrf <div class="card"> <div class="card-header"> <h3 class="card-title">Added Product Attributes</h3> </div> <!-- /.card-header --> <div class="card-body"> <table id="products" class="table table-bordered table-striped"> <thead> <tr> <th>ID</th> <th>Size</th> <th>SKU</th> <th>Price</th> <th>Stock</th> <th>Actions</th> </tr> </thead> <tbody> @foreach ($productdata['attributes'] as $attribute) <!-- getting the attribute fields ids --> <input style="display: none;" type="text" name="attrId[]" value="{{$attribute['id']}}"> <tr> <td>{{$attribute['id']}}</td> <td>{{$attribute['size']}}</td> <td>{{$attribute['sku']}}</td> <td> <input type="number" name="price[]" value="{{$attribute['price']}}" required=""> </td> <td> <input type="number" name="stock[]" value="{{$attribute['stock']}}" required=""> </td> <td> @if($attribute['status']==1) <a class="updateAttributeStatus" id="attribute-{{$attribute['id']}}" attribute_id="{{$attribute['id']}}" href="javascript:void(0)"><i class="fas fa-toggle-on" aria-hidden='true' status="Active"></i></a> @else <a class="updateAttributeStatus" id="attribute-{{$attribute['id']}}" attribute_id="{{$attribute['id']}}" href="javascript:void(0)"><i class="fas fa-toggle-off" aria-hidden='true' status="Inactive"></i></a> @endif <a href="javascript:void(0)" class="confirmDelete" record="attribute" recordid="{{$attribute['id']}}" title="Delete Product"><i class="far fa-trash-alt text-danger"></i></a> </td> </tr> @endforeach </tbody> </table> </div> <!-- /.card-body --> <div class="card-footer"> <button type="submit" class="btn btn-warning">Update Attributes</button> </div> </div> <!-- /.card --> </form>
ProductsController.php
public function editAttributes(Request $request, $id) { if ($request->isMethod('post')) { $data = $request->all(); // echo "<pre>"; // print_r($data); // die; foreach ($data['attrId'] as $key => $attr) { if (!empty($attr)) { ProductsAttribute::where(['id' => $data['attrId'][$key]])->update(['price' => $data['price'][$key], 'stock' => $data['stock'][$key]]); } $success_message = "Product attributes has been updated successfully!"; session::flash("attSuccess_message", $success_message); return redirect()->back(); } } }
Advertisement
Answer
Upgrade your ProductsController.php
as following, it might work.
public function editAttributes(Request $request, $id) { if ($request->isMethod('post')) { $data = $request->all(); foreach ($data['attrId'] as $key => $attr) { if (!empty($attr)) { ProductsAttribute::where(['id' => $data['attrId'][$key]]) ->update(['price' => $data['price'][$key], 'stock' => $data['stock'][$key]]); } } // You can check all attributes, updated or not if you wish $success_message = "Product attributes has been updated successfully!"; session::flash("attSuccess_message", $success_message); return redirect()->back(); } }