如何使用 laravel 5.4 将多个表单字段保存到数据库中

How to save multiple form field into database using laravel 5.4

我有多个由 jquery 生成的表单字段,但只将最后一条记录保存到数据库

<tr>
    <td><input class="form-control" id="quantity[]" name="quantity[]" placeholder="Quantity" type="text"></td>
    <td><input class="form-control" id="price[]" name="price[]" placeholder="Enter Pice" type="text"></td>
</tr>

生成附加表单字段的 jquery 在这里

 $('#priceTable').append(
     "<tr> <td><input class='form-control' id='quantity[1]' name=\"quantity[]\" placeholder='Quantity' type='text'></td><td><input class='form-control' name=\"price[]\"  id='price[1]' +' placeholder='Enter Pice' type='text'></td> </tr>"\
 );

php 页面的结果如下所示

array([quantity] => Array ( [0] => 45 [1] => 60 ) [price] => Array ( [0] => 45000 [1] => 60000 )

假设post它到数据库的循环但是post只有一个到数据库

 for ($i=0; $i <= $noQuantity; $i++ )
        {
           if(!empty($price)){
               $data = [
                   'price' => $priceRe[$i],
                   'quantity' => $quantity[$i]
               ];
               $price->create($data);
               return redirect('/admin123/category');
           }

         }

它只创建一个,因为您在循环内重定向,所以它创建第一个,然后将用户发送到另一个页面。尝试将其移出:

for ($i=0; $i <= $noQuantity; $i++ )
{
    if(!empty($price)){
        $data = [
            'price' => $priceRe[$i],
            'quantity' => $quantity[$i]
        ];
        $price->create($data);
    }
}
return redirect('/admin123/category');