首页 > 解决方案 > 如何将数组请求保存在数据库中

问题描述

我的桌子

$table->increments('id');
$table->string('productname');
$table->string('qty');
$table->string('price');
$table->string('priceinword');
$table->timestamps();

表单像这样来到控制器

  array:5 [▼
  "_token" => "za1Pzkwihk7trQcf2xWIxVjIzPBycl5Ix8dYYTjD"
  "productname" => array:2 [▼
    0 => "product 1"
    1 => "product 2"
  ]
  "qty" => array:2 [▼
    0 => "1"
    1 => "1"
  ]
  "price" => array:2 [▼
    0 => "123"
    1 => "321"
  ]
  "priceinword" => array:2 [▼
    0 => "one two three"
    1 => "three two one"
  ]
]

如何将数据保存到数组中的产品表中如何解决此查询

标签: laravellaravel-5laravel-5.6

解决方案


用于

for($i = 0; i<count($request->productname); $i++)
{
    Product::create([
        'productname' => $request->productname[$i],
        'qty' => $request->qty[$i],
    ]);
}

推荐阅读