I have a form which insert multiple products here it's my 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() !!}
The end results is an array.
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"
]
]
and here is my controller
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' );
}
How to insert each item as row in my table