问题描述
我正在使用具有大量计算功能的API,最后有一个很大的Foreach循环使用了将近100个数据库字段.
I am using a API's with lot's of calculation almost 100 database fields at the end with a big Foreach loop.
在每次迭代中,我都会在数据库中插入数据.我想在末尾一次插入数据(类似于CodeIgniter中的批处理插入).
In every iteration i insert data in database. I want to insert data in once at the end (Batch Insert like in CodeIgniter).
任何人都知道如何在迭代结束时插入所有数据.而不是每次迭代,它都会在数据库中插入行.
Any body have idea how to insert all data at the end of iteration. instead of every iteration it insert row in database.
我想在循环末尾插入数据.任何帮助或想法表示赞赏.
I want to insert data at the end of loop. Any help or idea appreciated.
推荐答案
使用 insert()
批量插入的方法.首先,使用以下结构构建一个数组:
Use insert()
method for bulk insertion. First, build an array with this structure:
$data = [
['name' => 'John', 'age' => 25],
['name' => 'Maria', 'age' => 31],
['name' => 'Julia', 'age' => 55],
];
Then insert the data using Eloquent model:
Model::insert($data);
DB::table('table_name')->insert($data);
这篇关于在Laravel 5.2中批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!