Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert multiple records at once in laravel eloquent

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 DB query builder class, like

 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?

like image 935
devnull Ψ Avatar asked Jan 29 '23 23:01

devnull Ψ


1 Answers

You can do this with model:

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

But here insert is the same QB method.

like image 81
Alexey Mezenin Avatar answered Feb 05 '23 14:02

Alexey Mezenin