Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Eloquent: upsert for Bulk Operations

Insert or update multiple records efficiently with upsert.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Basic Upsert
  2. UpdateOrCreate
  3. FirstOrCreate
  4. Insert Ignore

Efficient bulk operations:

Basic Upsert

php
User::upsert([
    ['email' => 'john@example.com', 'name' => 'John'],
    ['email' => 'jane@example.com', 'name' => 'Jane'],
], ['email'], ['name']);

UpdateOrCreate

php
$user = User::updateOrCreate(
    ['email' => 'john@example.com'],
    ['name' => 'John Doe']
);

FirstOrCreate

php
$user = User::firstOrCreate(
    ['email' => 'john@example.com'],
    ['name' => 'John']
);

Insert Ignore

php
User::insertOrIgnore([
    ['email' => 'john@example.com'],
]);
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read