Batch insertion in Laravel 5.2

I use an API with a lot of calculations for almost 100 database fields at the end with a large Foreach loop.

In each iteration, I insert data into the database. I want to insert data once at the end (Batch Insert, as in CodeIgniter).

Any body has an idea how to insert all the data at the end of the iteration. instead of each iteration, it inserts a row into the database.

I want to insert data at the end of a loop. Any help or idea appreciated.

+4
source share
1 answer

Use the insert()method for volume insertion. First create an array with this structure:

$data = [
    ['name' => 'John', 'age' => 25],
    ['name' => 'Maria', 'age' => 31],
    ['name' => 'Julia', 'age' => 55],
];

Then paste the data using the Eloquent model:

Model::insert($data);

Or using the query builder:

DB::table('table_name')->insert($data);
+8

Source: https://habr.com/ru/post/1667859/


All Articles