laravel 在laravel eloquent中一次插入多条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47943344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:09:13  来源:igfitidea点击:

insert multiple records at once in laravel eloquent

laravellaravel-5eloquent

提问by devnull Ψ

I have multidimensional array and I want to insert all the data in one query with my model, I know I can do it with DBquery builder class, like

我有多维数组,我想用我的模型在一个查询中插入所有数据,我知道我可以用DB查询构建器类来做到这一点,比如

 DB::table('table')->insert([ 
     ['name' => 'foo'],
     ['name' => 'bar'],
     ['name' => 'baz']
 ]);

but how can I do it with model? Model::create()doesn't insert multiple records, also I don't want to insert items with loop. is it possible to do this with eloquent?

但是我怎么能用模型来做呢? Model::create()不插入多条记录,我也不想用循环插入项目。有可能用 eloquent 做到这一点吗?

回答by Alexey Mezenin

You can do this with model:

您可以使用模型执行此操作:

Model::insert([ 
   ['name' => 'foo'],
   ['name' => 'bar'],
   ['name' => 'baz']
]);

But here insert is the same QB method.

但这里插入是相同的 QB 方法。