我正試圖使用陣列向一個透視表order_serrvice插入資料。 我遵循這段代碼。https://blog.quickadminpanel.com/master-detail-form-in-laravel-jquery-create-order-with-products/
OrderController:
$order = Order::create($data) 。
$services = $request->input( services', [])。
$quantities = $request->input('quants', [] )。)
for ($service = 0; $service < count($services); $service ) {
if ($services[$service] != ''/span>) {
$order->services()->attach($services[$service], [' quantity' => $quantities[$service]])。)
}
刀片頁面:
<tbody>
<tr id='addr0'>
<td>1</td>
<td>
<select name="services[]" class="form-control" >
<option value="">-- choose service --</option>
@foreach ($services as $service)
<option value="{{ $service->id }}">
{{ $service->name }}">"。(${{ number_format($service->price, 2) }})
</option>
@endforeach
</select>
{{--<input type="text" name='service[]' placeholder='Enter Product Name' class="form-control"/> --}}
</td>
<td>< input type="number" name='qty[]' placeholder='Enter 數量' class="form-control qty" step="0" min="0"/> </td>
<td><input type="number" name='price[]' placeholder='Enter Unit Price' class="form-control price" step="0. 00" min="/span>0"/></td>
<td><input type="number" name='total[]' placeholder='0.00' class="form-control total" readonly/> < /td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
用dd($request):
"services" => array:2 [ ▼
0 => "1"/span>
1 => "2"/span>
]
"qty" => array:2 [ ▼
0 => "27"[/span]。
1 => "2"/span>
]
"price" => array:2 [ ▼
0 => "489"
1 => "4"
]
"total" => "23647.69" ]
"sub_total"=> "13211.00"
"vat" => "10436.69"。
dd($services[$service]):
"1"
錯誤:
ErrorException 未定義的偏移量:0 $order->services()->attach($services[$service], ['quantity' => $quantities[$service]]);
有誰能幫忙嗎?
uj5u.com熱心網友回復:
不要用isset檢查null。你的for回圈從0開始,所以最初它檢查的是第2個值,而你的陣列沒有這個索引。所以改變回圈開始時的迭代值
for ($service=1; $service <= count($services); $service ) {
if (isset($services[$service]) {
$order->services()->attach($services[$service], [' quantity' => $quantities[$service]])。)
}
uj5u.com熱心網友回復:
我解決了這個問題,這是一個簡單的錯誤,我只是在OrderController中用qty替換了Quantity
。 $order = Order::create($data) 。
$services = $request->input( services', []);
$quantities = $request->input('qty', [] ) 。
for ($service = 0; $service < count($services); $service ) {
if ($services[$service] != ''/span>) {
$order->services()->attach($services[$service], ['qty'/span> => $quantities[$service]])。)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322441.html
標籤:
上一篇:以多維陣列作為形式引數的函式
