laravel - 如何将数组请求保存在数据库中
问题描述
我的桌子
$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"
]
]
如何将数据保存到数组中的产品表中如何解决此查询
解决方案
用于
for($i = 0; i<count($request->productname); $i++)
{
Product::create([
'productname' => $request->productname[$i],
'qty' => $request->qty[$i],
]);
}
推荐阅读
- java - 从java中的串口读取时获取垃圾值
- java - 如何使用 JPA 与关联类建立递归的多对多关系?
- nfc - NFC 贴纸以启动具有唯一 URL 的网站
- ibeacon - WebBluetooth可以发送信标吗
- c++ - 如何将 PHYSICAL_MONITOR 与监视器 DeviceID 关联
- docker - 无法通过“network_mode:host”访问容器
- dialogflow-es - 无法通过控制台创建新的 Dialogflow 代理
- reactjs - TypeError: _ClassName__WEBPACK_IMPORTED_MODULE_7__.ClassName 不是构造函数
- python - Beautiful Soup - 查找所有包含已知字符串的类
- php - 包含文件夹中的所有文件 - PHP