Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

randm's avatar

Is there a way to perform bulk insert if record does not exists in Laravel 5.1?

I want to use a method like updateOrCreate() but instead of inserting one record at a time I would like to insert multiple records at the same time.

How can I perform this bulk insert while checking if a record already exists or not?

0 likes
7 replies
JarekTkaczyk's avatar

@malhayek To be specific - there are no bulk actions in eloquent. No matter what, you can just cut the code, but real insert/update/whatever will not be bulk, but separate action for each single model/row.

That said, you can simply run array_map or foreach with updateOrCreate for each item in the loop.

jlrdw's avatar

Can you give more detail like how many at a time and that sort of thing? And don't rule out stored procedures.

JarekTkaczyk's avatar
Level 53

@malhayek If we talk about eloquent, then certainly yes.

If you worry about performance of this action, then you can do it in 4 steps:

  1. get the existing rows with single query and orWheres
  2. reduce the array of values to update/insert to separate those arrays: one to be inserted and other created
  3. run bulk insert DB::table('your_table')->insert($array_of_arrays_with_values)
  4. build update query with case when ... then ... statement

This way you end up with 3 queries, but you need to take care of the logic above and also make sure to add proper fields to both point 3 and 4 (eg. timestamps, since eloquent will not be in action here)

1 like
randm's avatar

@JarekTkaczyk thank you. Since the records are about 5-25 each time I will just perform updateOrCreate() for every record.

Thank you

mayank_dudakiya's avatar

Laravel has introduced upsert in Laravel 8: https://laravel.com/docs/10.x/eloquent#upserts

So basically, upsert will update multiple records and insert new one if matching not found.

Notes: Number of column should be same in requested array including id with blank value to insert new records. Check the below example:

In the following array first record will be updated and second array will be inserted into the database:

array:2 [▼
  0 => array:5 [▼
    "id" => 23
    "job_card_id" => 7
    "name" => "Oil change"
    "quantity" => 10
    "price" => 20
  ]
  1 => array:5 [▼
    "id" => null
    "job_card_id" => 7
    "name" => "Test"
    "quantity" => "5"
    "price" => "10"
  ]
]
JobCardService::upsert(
    $request->only('services'), // All fields
    ['id'], // Unique fields
    ['job_card_id', 'name', 'quantity', 'price'], // Update or Insert columns in db
);

Please or to participate in this conversation.