簡體   English   中英

Laravel 5.4:將每個項目作為一種表格的行插入

[英]Laravel 5.4: insert each item as row from one form

我有一個插入多個產品的表格,這是我的表格

{!! Form::open(['route'=>'export-list.store']) !!}
        @foreach($orderis as $orderi)
           {!! Form::hidden('product_id[]', $orderi->productId->id) !!}
           {!! Form::hidden('quantity[]', $orderi->quantity) !!}
               <tr>
                  <td>{!! $sn++ !!}</td>
                  <td width="5%">
                  {!! Html::image('images/products/'. $orderi->productId->image, 'thumbs-'.$orderi->productId->name, ['height' => 70]) !!}
                  </td>
                  <td>{!! $orderi->product_name !!}</td>
                  <td>{!! $orderi->quantity !!}</td>
                  <td>
                     <div class="col-md-5">
                        <div class="form-group-inner">
                       {!! Form::text('inventory[]', null, ['class'=>'form-control']) !!}
                        </div>
                     </div>
                 </td>
                 <td>{!! $orderi->productId->price !!}</td>
                 <td>{!! $orderi->productId->price * $orderi->quantity !!}</td>
               </tr>
           @endforeach
               <tr>
                 <td colspan="9">{!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}</td>
               </tr>
{!! Form::close() !!}

最終結果是一個數組。

array:4 [▼
  "_token" => "cpua1RzKKP4vvklB99HafAdMQ65TSxOWQVQ5r3ye"
  "product_id" => array:3 [▼
    0 => "856"
    1 => "857"
    2 => "858"
  ]
  "quantity" => array:3 [▼
    0 => "9"
    1 => "8"
    2 => "2"
  ]
  "inventory" => array:3 [▼
    0 => "8"
    1 => "2"
    2 => "6"
  ]
]

這是我的控制器

public function store( Request $request ) {
        $input = $request->all();

        //Check for the needed quantity
        $quantity        = $input['quantity'];
        $inventory       = $input['inventory'];
        $needed_quantity = $quantity - $inventory;
        //Get the product information
        $productInfo = ( new Product() )->find( $input['product_id'] );
        $totalPrice  = $productInfo->price * $needed_quantity;


        ( new PurchaseOrder() )->create( [
            'product_id'      => $input['product_id'],
            'product_name'    => $productInfo->translate( 'ar' )->name,
            'product_image'   => $productInfo->image,
            'needed_quantity' => $needed_quantity,
            'unit_price'      => $productInfo->price,
            'total_price'     => $totalPrice,
            'barcode'         => $productInfo->barcode,
        ] );

        return redirect()->route( 'order-detail.index' )->with( 'status', 'Created successfully' );
    }

如何將每個項目作為表中的行插入

非常感謝@SNAPEY,他幫助我解決了問題

當我以其他方式命名字段時,更容易...如

@foreach($orderis as $orderi)
        {!! Form::hidden('products[' . $loop->index . '][product_id]', $orderi->productId->id) !!}
        {!! Form::hidden('products[' . $loop->index . '][quantity]', $orderi->quantity) !!}
        {!! Form::text('products[' . $loop->index . '][inventory]', null, ['class'=>'form-control']) !!}           
@endforeach

所以最終結果是這樣的

array:2 [▼
  "_token" => "muw3phvtnjnJnjBYNqbp0A5f6Pk5St1BkydKPGG7"
  "products" => array:3 [▼
    0 => array:3 [▼
      "product_id" => "856"
      "quantity" => "9"
      "inventory" => "2"
    ]
    1 => array:3 [▼
      "product_id" => "857"
      "quantity" => "8"
      "inventory" => "3"
    ]
    2 => array:3 [▼
      "product_id" => "858"
      "quantity" => "2"
      "inventory" => "4"
    ]
  ]
]

像這樣用foreach在我的控制器中管理數據非常容易

$eachProducts = $input['products'];

foreach ( $eachProducts as $each_product ) {
    $quantity        = $each_product['quantity'];
    $inventory       = $each_product['inventory'];
    ......
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM