[英]How can I insert multiple rows to the database in Laravel
I tried to save multiple rows to the database with one query, without success. 我试图通过一个查询将多行保存到数据库,但没有成功。 I used a for loop but it can't retrieve the value of the inputs that are generated using jQuery after setting the number of rows.
我使用了for循环,但在设置行数后无法检索使用jQuery生成的输入的值。
Controller 调节器
public function store(Request $request)
{
$nbrrowsol = $request->get('nbrrowsol');
for ($i = 1; $i < $nbrrowsol; $i++) {
$commande = new Commande();
$commande->nom_client = $request->get('nomclient');
$commande->organisme = $request->get('organisme');
$commande->adresse = $request->get('adresse');
$commande->email = $request->get('email');
$commande->tel1 = $request->get('tel');
$commande->tel2 = $request->get('tel2');
$commande->fax = $request->get('fax');
$commande->commercial = $request->get('commercial');
$commande->date_reception = $request->get('datereception');
$commande->date_prelevement = $request->get('dateprelev');
$commande->saved_by = $request->get('savedby');
$commande->code = $request->get('codesol' + $i);
$commande->nature = $request->get('naturesol' + $i);
$commande->reference_cli = $request->get('reference_clisol' + $i);
$commande->profondeur = $request->get('profondeursol' + $i);
$commande->culture = $request->get('culturesol' + $i);
$commande->variete = $request->get('varietesol' + $i);
$commande->gps = $request->get('gpssol' + $i);
$commande->analyse_demande = $request->get('analysedemandesol' + $i);
$commande->valide = $request->get('checkvalidee');
$commande->save();
}
return redirect('gestion_commandes/create');
}
I get the following error after the user submits for saving data. 用户提交保存数据后出现以下错误。
A non-numeric value encountered
遇到非数字值
I think that I didn't use (for) correctly or the form of: 我认为我没有正确使用(for)或以下形式:
$commande->code = $request->get('codesol' + $i);
You seem to be fetching input using +
addition operator which is for javascript, in PHP .
您似乎正在使用PHP中用于javascript的
+
加法运算符来获取输入.
operator is used for concatenation, also you'll need to fetch inputs it using the index of your inputs array, for example when your <input name="nomClient[]">
运算符用于连接,您还需要使用输入数组的索引来获取输入,例如,当您的
<input name="nomClient[]">
$request->get('nomclient['. $i . ']')
First of all to concat a string in PHP is done using .
首先,使用PHP在PHP中连接字符串
.
(dot) notation, not +
. (点)表示法,而不是
+
。 So this 'codesol'+$i
should become 'codesol' . $i
所以这个
'codesol'+$i
应该变成'codesol' . $i
'codesol' . $i
and so on for the rest. 'codesol' . $i
,以此类推。
If you want to insert multiple rows directly, then take a look at this example. 如果要直接插入多行,请看一下此示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.