我正試圖使用陣列向樞軸表order_serrvice插入資料。 我遵循這段代碼。https://blog.quickadminpanel.com/master-detail-form-in-laravel-jquery-create-order-with-products/
列qty和服務被正確插入,但價格沒有,我不知道為什么?
order_service表:
Schema:: create('order_service', function (Blueprint $table) {
$table-> foreignId('service_id')。
$table-> foreignId('order_id')。
$table->float('price')->默認(0)。
$table->整數('qty')->默認(0) 。
$table->primary(['service_id','order_id'])。)
OrderController:
$services = $request-> input('services', [] ) 。
$quantities = $request->input('qty', [] )。)
$price=$request->input('price', [] )。)
for($service=0; $service < count($services); $service ) {
if ($services[$service] != ' ') {
$order->services()->attach($services[$service], ['qty'/span> => $quantities[$service]], ['price' => $price[$service] ])。
}
用dd($request):
"services"/span> => array:2 [▼
0 => "3"/span>
1 => "2"/span>
]
"qty" => array:2 [ ▼
0 => "861"/span>
1 => "748"/span>
]
"price" => array:2 [ ▼
0 => "20"
1 => "40"
dd($price[$service])
"20"
資料基數outbut
| order_id | service_id |price |qty|
| -------- | -------------- |______|____|__。
| 2074 | 2 |0.00 |748 |
| 2074 | 3 |0.00 |861 |
誰能幫幫我?
uj5u.com熱心網友回復:
在你的控制器中,你誤用了attach()方法:
$order->services()->attach(
$services[$service] 。
['qty' => $quantities[$service] ]。
['price' => $price[$service ]] //你的陣列在這里有問題,它只需要第二個引數中的一個。
);
attach()方法應該這樣使用:
$order->services()->attach(
$services[$service], [
'qty' => $quantities[$service] 。
'price' => $price[$service].
);
第二個引數是一個陣列,包含所有額外的樞軸值。
uj5u.com熱心網友回復:
你好,請嘗試一下這個代碼,在單個陣列中附加值
$order->services()->attach($services[$service], ['qty' => $quantities[$service], 'price' => $price[$service]]);轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331796.html
標籤:
上一篇:從配置中檢索資料。PHP檔案
下一篇:使用Xampp80埠
