我正在尝试使用laravel将循环数据插入数据库,但它返回以下错误:
Grammar.php第118行中的ErrorException: 传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是类型数组,给定的字符串,在C:\ xampp \ htdocs \ laravel \ App \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query中调用\#Grammars \ Grammar.php在678行并定义了
查看:
<input name="product_id[]" type="hidden" class="rid" value="13">
<input type="hidden" name="product_name[]" value="Popins"><input type="hidden" name="product_code[]" value="2323">
<input name="unit_price[]" type="hidden" value="170">
<input name="quantity[]" type="text" value="2" id="" >
控制器
for($i=0; $i<count($product_name);$i++){
$newSafety = new saleItems;
$newSafety->product_id = $request->unit_price;
$newSafety->sale_id = $request->unit_price;
$newSafety->product_code = $request->product_code;
$newSafety->product_name = $request->product_name;
$newSafety->quantity = $request->quantity;
$newSafety->unit_price = $request->unit_price;
$newSafety->gross_total= $request->unit_price;
$newSafety->save();
}
答案 0 :(得分:0)
看起来您传递请求的每个输入都是数组,并且您尝试将它们分配给$ newSafety对象的非数组属性。
试试这个:
for($i=0; $i<count($product_name);$i++){
$newSafety = new saleItems;
$newSafety->product_id = $request->unit_price[i];
$newSafety->sale_id = $request->unit_price[i];
$newSafety->product_code = $request->product_code[i];
$newSafety->product_name = $request->product_name[i];
$newSafety->quantity = $request->quantity[i];
$newSafety->unit_price = $request->unit_price[i];
$newSafety->gross_total= $request->unit_price[i];
$newSafety->save();
}