MYSQL插入laravel 5.5请求控制器

时间:2017-11-27 22:33:52

标签: php mysql laravel

我有以下代码循环通过动态创建的下拉菜单,并将每个产品添加到订单数据库。我已经将这些页面移动到了laravel但是仍然坚持让这部分工作。以下是我的Legacy Code

// prepare and bind
$stmt = $conn->prepare("INSERT INTO orders (customer_id, order_id, 
enrolment_id, product_description, delivery_mode, course_cost) VALUES (?, ?, 
?, ?, ?, ?);");
$stmt->bind_param("iisssi", $payment_cid, $mysql_id, $enrolment_id, 
$mysql_desc, $mysql_mode, $mysql_cost);
$fetchid = mysqli_insert_id($conn);


$result = array_map(function($d, $c, $m) { 
return array('product_description'=>$d, 'course_cost'=>$c, 
'delivery_mode'=>$m); 
}, $product_description, $course_cost, $delivery_mode
);  

// set parameters and execute

foreach($result as $item){
$mysql_id = $fetchid;
$mysql_desc = $item['product_description'];
$mysql_mode = $item['delivery_mode'];
$mysql_cost = $item['course_cost'];
$stmt->execute();
}

$stmt->close();
$conn->close();

我将其转换为Laravel。这是我尝试使用CRUD控制器进行保存。顶部保存单个数据输入正常但是当有多个产品时我遇到了错误。

Hey Serge, yes all code is massive to paste in here so snippets super brief.  My product_description saving snippet is example as this is where I get stuck.

以下代码我知道错误,因为它是单个保存和我尝试循环的混搭:

$orders = new Order([
'customer_id' => $customers{'id'},
'course_cost' => $request->get('product_cost'),
'order_id' => $customers{'id'},
'enrolment_id' => $customers{'enrolment_id'},
'product_description' => $request->get('product_description'),
'delivery_mode' => $request->get('delivery_mode'),
'course_cost' => $request->get('course_cost'),
]);

$dataSet = [];
foreach ($orders as $item) {
$dataSet[] = [
'product_description'  => $request->get('product_description'),   
];
}
DB::table('orders')->insert($dataSet);
return redirect('/customers');
}

当前代码出错 enter image description here

0 个答案:

没有答案