我正在尝试将数据保存在具有多个数组的Laravel中。
数组如下所示:
Array
(
[0] => Array
(
[client_personnel_name] => Ron
[client_id] => 52
[client_personnel_email] => abc@gmail.com
)
[1] => Array
(
[client_personnel_name] => John
[client_id] => 52
[client_personnel_email] => abc@gmail.com
)
)
当我保存这些数据时:
$personnel = ClientsPersonnel::create($client_personnel);
$personnel->save();
在调试时,要创建要插入的数据。这就是我在存储发送数据的属性中得到的内容。
[attributes:protected] => Array
(
[updated_at] => 2015-04-23 06:53:05
[created_at] => 2015-04-23 06:53:05
[id] => 2
)
如何保存具有多个数组的数据?
发布于 2015-04-22 23:12:57
可以使用DB::insert()
,如下所示:
DB::table('client_personnel')->insert(array($client_personnel));
作为另一种选择,您可以使用类似的循环来完成此操作。
foreach ($personnels as $personnelAttributes) {
$personnel = new ClientsPersonnel($personnelAttributes);
$personnel->save();
}
致以敬意,
发布于 2015-04-22 23:10:00
Laravel雄辩不具有批量更新或插入功能。您需要为每个子数组创建新的ClientsPersonnel并单独保存它。
发布于 2015-04-22 23:14:09
只需循环数组并分别插入:
foreach ($client_personnel as $client) {
ClientsPersonnel::create($client);
}
https://stackoverflow.com/questions/29816069
复制