S
S
suhuxa12017-08-08 13:44:18
Laravel
suhuxa1, 2017-08-08 13:44:18

How to add a lot of information to DB via updateOrCreate in laravel?

It is necessary to add\update 1000 records at once. One record at a time to update through the loop is blasphemy. How to make it possible to add / update 1000 records at once through the updateOrCreate function, in one go, so to speak?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Melikhov, 2017-08-08
@amelihovv

With Model::updateOrCreate() you can only work with one entry. To add 1000 records at once, you need to use the Model::insert() method. You can pass an array of records to it at once. Just keep in mind that you will need to manually fill in the created_at and updated_at fields.
If 1000 records in size turns out to be a lot, then you can either increase the limit in the database settings, or record in parts (chunks) of 100 records, for example.
If you have a unique index on some field in your table, you can use insert on duplicate key. Here is the finished package https://github.com/melihovv/insert-on-duplicate-key (only mysql and sqlite support).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question