Find or Create with Eloquent

Find or Create with Eloquent

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Find or Create with Eloquent

When working with Laravel's Eloquent ORM, a common pattern emerges when handling data that might already exist in the database: you need to either fetch an existing record by its primary key or create a new one if it doesn't exist. The traditional approach involves chaining conditional logic, often looking like this:

$user = User::find($id);
if (!$user) {
    $user = new User;
}

While functional for simple cases, this pattern breaks down quickly when you need to populate attributes or save the model in a single operation. More importantly, it introduces unnecessary branching logic that can clutter your code and obscure intent. The question is whether Eloquent offers a cleaner way to express "find or create" without writing explicit if statements every time.

The answer lies in Eloquent's firstOrCreate method. This helper accepts the model name (or an instance), the key attributes used for existence checking, and an associative array of attributes to set on the model. If a record exists matching the keys, Eloquent retrieves it; otherwise, it creates a new instance with the provided values and saves it immediately.

Here is how you would refactor the previous example:

$user = User::firstOrCreate(
    ['email' => $email], 
    [
        'name' => $name,
        'password_hash' => Hash::make($password),
    ]
);

This single line handles both retrieval and creation atomically. However, there are nuances to consider when deciding between find/create, firstOrCreate, and similar utilities.

Atomicity and Race Conditions

The primary advantage of firstOrCreate is that it performs the check-and-create operation in a single database query cycle (or two if using optimistic locking strategies internally). In high-concurrency scenarios, this can prevent race conditions where two requests might create duplicate records simultaneously. If you were to use separate find and create calls without proper transactional control, you risk creating duplicates when the first request fails to find a record but the second request succeeds before the first saves.

Handling Updates

One of the most frequent use cases for this pattern is "upserting" data—ensuring a record exists and updating it if necessary. With firstOrCreate, you can provide an array of attributes that should be merged into the existing record (if found) or used to initialize a new one. This makes it ideal for scenarios like:

$user = User::firstOrCreate(
    ['email' => $email], 
    [
        'name' => $request->input('name'),
        'role' => 'user',
        'created_at' => now(), // Ensures timestamps are set on creation
    ]
);

// If the user already existed, you might want to update specific fields only if they changed
if ($user->email !== $request->input('email')) {
    // Handle email change logic here
}

Note that firstOrCreate does not automatically update all attributes passed in the second argument if a record exists. It only sets the values provided. To perform a full update, you typically need to explicitly assign the attributes after retrieval:

$user = User::find($id);
if (!$user) {
    $user = new User;
}

$user->name = $name;
$user->email = $email;
$user->save();

This explicit approach gives you more control over which fields are updated, avoiding unintended side effects like clearing out sensitive data or resetting timestamps unexpectedly.

Trade-offs

While firstOrCreate is convenient, it doesn't replace the need for explicit updates in all cases. If your logic requires complex validation before saving, or if you need to conditionally update only specific fields based on input, sticking with separate find and create/update calls might be clearer. Additionally, firstOrCreate always attempts to save a new record if none is found, which means it will trigger events like created in the model lifecycle, whereas a simple find does not.

Understanding these distinctions helps you choose the right tool for the job, keeping your codebase maintainable and efficient.

Our network