Inside the loop the $qty[$key] and $action[$key] both works fine but I am facing this error with the $batch_id[$key]. The loop I used for test is just two indexes. and the dd of $data as shown below. I also noticed that the quantity gets updated for the first index only in the database, which means the issue is with the second index as it does not get updated.

I can't figure how come its not working only with the batch_id, anyone have an idea?

I tried to essign the $batch_id[$key] to a var and use this var but not that is not working too. and the dd(isset($batch_id[$key])); is true for both index[0] and index[1]

array:12 [▼ // app/Http/Controllers/AdjustmentController.php:201
       "_token" => "TWFJuqmjro2Jdhovo28vAw0pjePLfEHp2bI0gcm1"
       "warehouse_id" => "2"
       "stock_count_id" => "46"
       "qty" => array:2 [▼
        0 => "1"
        1 => "3"
       ]
      "action" => array:2 [▼
       0 => "+"
       1 => "-"
      ]
      "product_id" => array:2 [▼
       0 => "114"
       1 => "114"
      ]
      "product_code" => array:2 [▼
       0 => "36089125"
       1 => "36089125"
      ]
      "batch_id" => array:2 [▼
       0 => "761"
       1 => "762"
      ]
      "total_qty" => "4"
      "item" => "2"
      "note" => null
      "reference_no" => "adr-20231210-082622"
     ]
@foreach($names as $key=>$name)
 <tr>
   <td>{{$name}}</td>
   <td>{{$code[$key]}}</td>
   <td>{{$batch_id[$key]}}</td>
   <td><input type="number" class="form-control qty" name="qty[]" value="{{$qty[$key]}}" required step="any" /></td>
   <td class="action">                                                                 <select name="action[]" class="form-control act-val">                                                                       @if($action[$key] == '+')                                                                                <option value="+">{{trans("file.Addition")}}</option>                                                                          <option value="-">{{trans("file.Subtraction")}}</option>                                                                       @else                                                                             <option value="-">{{trans("file.Subtraction")}}</option>                                                                              <option value="+">{{trans("file.Addition")}}</option>                                                                          @endif
    </select>
   </td>
   <td>                                                                     <button type="button" class="ibtnDel btn btn-md btn-danger">{{trans("file.delete")}}</button>
     <input type="hidden" class="product-id" name="product_id[]" value="{{$product_id[$key]}}" />                                                              <input type="hidden" class="product-code" name="product_code[]" value="{{$code[$key]}}" />
     <input type="hidden" class="batch_id" name="batch_id[]" value="{{$batch_id[$key]}}"/>
    </td>
  </tr>
@endforeach


public function store(Request $request)
     {
        $data = $request->except('document');
        $product_id = $data['product_id'];
        $product_code = $data['product_code'];
        $batch_id= $data['batch_id'];
        $qty = $data['qty'];
        $action = $data['action'];
        foreach ($product_id as $key => $pro_id) {
            $lims_product_data = Product::find($pro_id);
            $myBatch=$batch_id[$key] ;
                $lims_product_warehouse_data = Product_Warehouse::where([
                    ['product_id', $pro_id],
                    ['product_batch_id', $myBatch],
                    ['warehouse_id', $data['warehouse_id'] ],
                ])->first();
                $lims_product_batch_data = ProductBatch::select('id', 'batch_no', 'qty', 'expired_date')->where('id',$myBatch)->first();
                if($action[$key] == '-'){
                    $lims_product_batch_data->qty -= $qty[$key];
                    $lims_product_data->qty -= $qty[$key];
                    $lims_product_warehouse_data->qty -= $qty[$key];
                }
                elseif($action[$key] == '+'){
                    $lims_product_batch_data->qty += $qty[$key];
                    $lims_product_data->qty += $qty[$key];
                    $lims_product_warehouse_data->qty += $qty[$key];
                }
                $lims_product_batch_data->save();
                $lims_product_data->save();
                $lims_product_warehouse_data->save();
                $batch_id = $lims_product_batch_data->id;

        }
        return redirect('qty_adjustment')->with('message', 'Data inserted successfully');
    }
0

There are 0 best solutions below