php - Laravel how to save data which in forloop -
i have data need save database. here's example controller:
public function saverequest(request $request){ $count = count($request->input('itemname')); $purchase = new prrequest; ($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); }
i got error after clicking save button.
errorexception in helpers.php line 671: preg_replace(): parameter mismatch, pattern string while replacement array
any solution? thank in advance.
you calling fill every iteration, s better build data batch insert
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 } db::table(table name) -> insert($data); <-- better, 1 query.
hope helps.
Comments
Post a Comment