我有需要保存到数据库中的数据。这是一个例子 现在我的控制器:
public function saveRequest(Request $request){
$count = count($request->input('itemname'));
$purchase = new PrRequest;
for ($i=0; $i<$count; $i++){
$data[] = array(
'pr_no' => $request->input('pr_no')[$i],
'pr_date' => $request->input('pr_date')[$i],
'department' => $request->input('department')[$i],
'remarks' => $request->input('remarks')[$i],
'notes' => $request->input('notes')[$i],
'requested_by' => $request->input('requested_by')[$i],
'item_id' => $request->input('itemname')[$i],
'qty' => $request->input('qty')[$i],
'type' => $request->input('type')[$i],
'uom' => $request->input('uom'),
'pd_date' => $request->input('pd_date')[$i]
);
$purchase->fill($data[$i])->save();
}
return Response::json($data, 200, array(), JSON_PRETTY_PRINT);
}
点击保存按钮后出错。
ErrorException in helpers.php line 671:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
任何解决方案?提前谢谢。
答案 0 :(得分:3)
您在每次迭代时调用fill,最好是构建数据然后执行批量插入
for ($i=0; $i<$count; $i++){
$data[] = array(
'pr_no' => $request->input('pr_no')[$i],
'pr_date' => $request->input('pr_date')[$i],
'department' => $request->input('department')[$i],
'remarks' => $request->input('remarks')[$i],
'notes' => $request->input('notes')[$i],
'requested_by' => $request->input('requested_by')[$i],
'item_id' => $request->input('itemname')[$i],
'qty' => $request->input('qty')[$i],
'type' => $request->input('type')[$i],
'uom' => $request->input('uom'),
'pd_date' => $request->input('pd_date')[$i]
);
$purchase->fill($data[$i])->save(); <-- remove this
}
DB::table(table name) -> insert($data); <-- this is better, one query.
希望这有帮助。