Laravel firstOrNew how to check if it's first or new?

Laravel firstOrNew how to check if it's first or new?

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

Stefan Bogdanescu

Founder & Senior Architect

The firstOrNew Dilemma: Determining User Existence in Laravel

Laravel's firstOrNew() method is a convenient shortcut for handling "upsert" logic without writing complex queries. It attempts to find a record matching specific criteria; if found, it returns the existing model instance. If not found, it creates a new instance populated with your provided data attributes. This pattern is excellent for idempotency, ensuring that duplicate entries don't clutter your database when processing bulk imports or user registrations.

However, the method signature itself does not provide a mechanism to distinguish between these two outcomes after execution. Unlike create() which returns a fresh model instance and first() which might return null, firstOrNew() always returns a populated Model object. This can lead to ambiguity in your application logic. You cannot simply check if the returned object is false or null, because the method guarantees an object will be returned regardless of whether it was just created or retrieved from storage.

To determine if the user existed before or if a new record was instantiated, you must rely on the database state immediately following the operation. The most reliable approach involves querying the primary key (usually id) and comparing it against your original data. If the returned model's ID is an empty string ("") or null, Laravel has just created a fresh record. Conversely, if the ID contains a numeric value, the record already existed in the database.

Here is a practical implementation pattern using the standard Eloquent syntax:

$user = App\User::firstOrNew([
    'email' => $userData->getEmail(),
    'name'  => $userData->getName(),
]);

// Force persistence to ensure we have the correct state for an ID check
$user->save();

if ($user->id) {
    // The user existed before; we updated them
} else {
    // This is a brand new user
}

While checking $user->id works perfectly fine for most scenarios, there is a critical detail often overlooked: the firstOrNew() method returns the model instance before it is saved to the database. In many Laravel versions and configurations, a newly instantiated model will have an empty or null ID until save() is called. Therefore, checking the property immediately after assignment without saving might yield a false positive (indicating a new user) even if you intended to update an existing one later in your logic flow. Always ensure persistence happens before making existence claims based on the primary key.

Another robust strategy involves explicitly saving the model and then reloading it from the database. This guarantees that the object properties reflect the actual state of the record in the storage layer, eliminating any confusion regarding uninitialized attributes:

$user = App\User::firstOrNew(['email' => $email]);
$user->save(); // Commit changes to ensure ID is populated

if ($user->id > 0) {
    // Existing user
}

It is also worth noting that if your application uses a custom primary key column name, you should adjust the property check accordingly (e.g., $user->custom_id). Additionally, if you are working within a transaction, ensure that the commit happens before evaluating the existence state, as uncommitted changes might not reflect in the id attribute depending on your database driver's behavior.

For advanced use cases where you need to know the outcome without modifying the model instance immediately, you can query the primary key directly using a raw SQL check or an explicit where clause before calling firstOrNew(). However, this adds complexity and defeats the purpose of the convenience method. Sticking to the save() followed by an ID check remains the most readable and maintainable solution for standard application logic.

If you require more sophisticated upsert handling with specific conflict resolution strategies beyond simple creation or update, reviewing how Laravel handles batch operations or using the updateOrCreate method alongside explicit existence checks might offer alternative architectural patterns depending on your specific version requirements.

Our network