如何在laravel6.0中插入多个数据
这是我的看法如何在laravel6.0中插入多个数据,laravel,Laravel,这是我的看法 @foreach($units as $unit) <tr> <td><input type="text" name="name{{$unit -> id}}" value="{{$unit -> name}}"/></td> <td><input type="text" name="value{{$unit -> id}}" value="{{$unit ->
@foreach($units as $unit)
<tr>
<td><input type="text" name="name{{$unit -> id}}" value="{{$unit -> name}}"/></td>
<td><input type="text" name="value{{$unit -> id}}" value="{{$unit -> value}}"/></td>
<td><button onclick = "delete(this)"></button></td>
</tr>
@endforeach
但我没有成功。
如果你有好的解决办法,请告诉我。
注意。这是我的简单解决方案,您的输入名称不是映射
for ($k = 1; $k <= count($unitArray); $k++){
$unitType = new UnitType();
$unitTypeName = 'name' . $k;
$unitTypeValue = 'value-' . $k;
$unitType -> name = $request->input($unitTypeName);
$unitType -> value = $request->input($unitTypeValue);
$unitType -> save();
}
for($k=1;$k name=$request->input($unitTypeName);
$unitType->value=$request->input($unitTypeValue);
$unitType->save();
}
我找到了解决方案。
我们可以对几个输入标记使用相同的名称,它们作为一个数组发布到控制器。
所以我们可以使用它
@foreach($units as $unit)
<tr>
<td><input type="text" name="name[]" value="{{$unit -> name}}"/></td>
<td><input type="text" name="value[]" value="{{$unit -> value}}"/></td>
<td><button onclick = "delete(this)"></button></td>
</tr>
@endforeach
@foreach($units作为$unit)
@endforeach
在控制器中
$name = $request -> name;
$value = $request -> value;
for($i = 0; $i<count($name);$i++){
$unit=new UnitType();
$unit->name=$name[$i];
$unit->value=$value[$i];
$unit->save();
}
$name=$request->name;
$value=$request->value;
对于($i=0;$iname=$name[$i];
$unit->value=$value[$i];
$unit->save();
}
再次感谢您,大卫先生。谢谢您的努力。大卫先生,我的电脑无法使用。
$name = $request -> name;
$value = $request -> value;
for($i = 0; $i<count($name);$i++){
$unit=new UnitType();
$unit->name=$name[$i];
$unit->value=$value[$i];
$unit->save();
}