php-如何将其保存在数据库中
作者:互联网
在我的数据库中,我有ID,product_id,company_name,service,qty,delivery_cost,delivery_date,order_status等列.
我认为我使用了Jquery和HTML,jquery在单击添加更多按钮时动态添加了更多的product_id,service,delivery_cost,qty,delivery_date,order_status输入字段.在提交表单时,我在做dd($allData)时将其输入控制器
我的问题是如何将这些数据保存在数据库中
array:8 [▼
"_token" => "gSddIeA11OBV60xU9YiDXn8fmsfkwxlQ85QmDdkQ"
"service" => array:3 [▼
0 => "cement"
1 => "iron"
2 => "steel"
]
"qty" => array:3 [▼
0 => "5"
1 => "44"
2 => "5"
]
"delivery_cost" => array:3 [▼
0 => "5465"
1 => "553"
2 => "554"
]
"delivery_date" => array:3 [▼
0 => "2016-12-16"
1 => "2016-12-08"
2 => "2016-12-17"
]
"order_status" => "Confirm"
"delivery_vehicle" => array:1 [▼
0 => "Self_elivery"
1 => "Self_elivery"
2 => "Self_elivery"
]
]
public function store(Request $request)
{
$allData= $request->all();
dd($allData);
$product = new Order;
}
我尝试这个
public function store(Request $request)
{
$date = $request->get('delivery_date');
$cost = $request->get('delivery_cost');
$service = $request->get('service');//add quotes next to service
foreach($date as $deliveryDate)
{
foreach($cost as $proAmount){
$db = new Order;
$db->delivery_date = $deliveryDate;
$db->amount = $proAmount;
$db->save();
}
}
return"ok";
}
我尝试过这种方式,但它多次存储相同的数据可能是由于循环内部的循环.我需要您的帮助才能将这些数据存储在数据库中
解决方法:
使用for()应该适合您:
$data = $request->all();
for ($i = 0; $i < count($data['delivery_date']); $i++) {
$db = new Order;
$db->delivery_date = $data['delivery_date'][$i];
$db->delivery_cost = $data['delivery_cost'][$i];
....
$db->save();
}
标签:laravel,php,jquery,database,laravel-5-3 来源: https://codeday.me/bug/20191026/1936441.html