mercredi 16 novembre 2016

Laravel Save Array In Database Sometimes Not Persisting

I have an application which on the view outputs details of an order.

Each line on the order the values which are delivered can change.

I've created a function sends the lines being dispatched to the controller via ajax and persists the data.

It works great when all of the lines have something being dispatched but when filtered to exclude lines with nothing being dispatched the system loops and generates an ID as if it had persisted to the database but doesn't actually save.

See my function on my controller.

public function testDespatch(Request $request)
    {

        $order = Order::where('OH_ORDER_NUMBER',round($request->order,0))->first();

       // return var_export($request->items);
        $input_items = array_filter($request->items, function($item){

            if($item['value'] != 0){ 
                return $item; 
            };

        }); // it will return an array

        $toDel = count($input_items);

            foreach($request->items as $de){

                if($order->items->contains($de['primary_key'])){

                    $item = OrderDetail::FindOrFail($de['primary_key']);

                    if($de['value'] <> 0){

                        $despatch = new DespatchOrder;
                        $despatch->OD_PRIMARY = $de['primary_key'];
                        $despatch->OD_ORDER_NUMBER = round($order->OH_ORDER_NUMBER,0);

                        if($item->OD_ENTRY_TYPE == 'S'){

                            $despatch->OD_STOCK_CODE = $item->OD_STOCK_CODE;  
                            $despatch->OD_DETAIL = $item->OD_DETAIL;  
                            $despatch->OD_ENTRY_TYPE = $item->OD_ENTRY_TYPE;  

                        }elseif($item->OD_ENTRY_TYPE == 'P'){

                            $despatch->OD_STOCK_CODE = $item->OD_PRICE_CODE;  
                            $despatch->OD_DETAIL = $item->OD_DETAIL;  
                            $despatch->OD_ENTRY_TYPE = $item->OD_ENTRY_TYPE;  

                        }else{

                            $despatch->OD_DETAIL = $item->OD_DETAIL;  
                            $despatch->OD_ENTRY_TYPE = $item->OD_ENTRY_TYPE;  

                        }

                        $despatch->OD_LINE_NUMBER = round($item->OD_LINE_NUMBER,0);
                        $despatch->QuantDel = $de['value'];
                        $despatch->DateDelivered = Carbon::now();
                        $despatch->DONE_BY = Auth::user()->id;
                        $despatch->TotalNoOfLines = $toDel;
                        $despatch->DespatchRef = strtotime(Carbon::now()) . '-' . Auth::user()->id . '-' . round($order->OH_ORDER_NUMBER,0);
                        $despatch->save();

                    }

                }

            }

            return 'success';

    }

Can anyone see why this might not persist?

for reference here's a dump of the an example array not persisting.

array (
  0 => 
  array (
    'name' => 'item[110145]',
    'value' => '200',
    'primary_key' => '110145',
  ),
  1 => 
  array (
    'name' => 'item[110146]',
    'value' => '0',
    'primary_key' => '110146',
  ),
)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire