81

I'm using Laravel's function firstOrNew() to create a new user or find and update an existing one.

How can I know, after the object is created, if it existed before or if it's a new object?

The idea is something like this:

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

if ($user->new) { // some way to check
    // user was created now
} else {
    //user already existed
}
patricus
  • 59,488
  • 15
  • 143
  • 145
sigmaxf
  • 7,998
  • 15
  • 65
  • 125

4 Answers4

146

firstOrNew()

This function will either return the first record from the database, or instantiate a new model instance that does not yet exist in the database. Therefore, if you'd like to check if the instance was pulled from the database or if it is a new instance, you can check the exists property (not function) on the model.

if ($user->exists) {
    // user already exists and was pulled from database.
} else {
    // user created from 'new'; does not exist in database.
}

The original question was about firstOrNew(), but a lot of people seem to come here for firstOrCreate(), as well. firstOrCreate() is different, and requires a different check.

firstOrCreate()

This function will either return the first record from the database, or create a new record in the database and return that. Since a newly created record would exist in the database, you can't check the exists property, because it'll be true in both instances. However, you can check the wasRecentlyCreated property. This property will be true if the current model instance was just created in the database, or false if it was already in the database.

if ($user->wasRecentlyCreated) {
    // user just created in the database; it didn't exist before.
} else {
    // user already existed and was pulled from database.
}
patricus
  • 59,488
  • 15
  • 143
  • 145
  • 17
    Be sure not to use `$user->exists()`. I just made that mistake. – JR Lawhorne Aug 21 '18 at 18:30
  • This does not work for me, because `exists` is essentially saying the record is present in the DB. what I really want to know is that the record was just inserted, or existed before the operation. Also, the new object returned does not actually have the data I want to use to create the DB record. Pengxer/Thomas Kim answer is really what I was looking for. – verboze Sep 17 '18 at 15:45
  • 7
    @verboze when using `firstOrNew` the model will not be inserted into the database. If you want to find the first model in the database and insert a new row if none exists, then you should use `firstOrCreate`. Then if you use `firstOrCreate` you can call `wasRecentlyCreated` and if that is true then the model was just created in the database, and if it is false then it existed before. – Nathan Heffley Aug 25 '19 at 00:10
17

You can check if your user was recently created.

if ($user->wasRecentlyCreated) {
    // new user
} else {
    // user already exists
}

(Source: Thomas Kim from this answer)

mhellmeier
  • 1,982
  • 1
  • 22
  • 35
  • 11
    wasRecentlyCreated is only useful in the context of firstOrCreate/updateOrCreate, and indicates if the model was just created in the database. firstOrNew doesn't save anything to the db by itself and will not set wasRecentlyCreated. – Simon Groenewolt Apr 24 '18 at 07:53
  • OP asked for firstOrNew, not this – Rotimi May 12 '22 at 10:33
6

If you created the model in the current lifecycle, then the model's wasRecentlyCreated attribute will be set to true. Otherwise, that attribute will be set to false.

In other words, lets say you have a user with the email, test@yopmail.com

$user = User::firstOrCreate(['email' => 'test@yopmail.com']);
var_dump($user->wasRecentlyCreated);

// the above will dump out false because this entry already existed otherwise true.

3

You can always check for an ID (or any other unique primary key) in the model.

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

if($user->id) {
    // The user exists and was retrieved from the database...
}

if (!$user->id) {
    // The user was not found in the database and a new User model was created...
}

It is important to keep in mind that after you persist the model using $user->save(), you will NOT be able to use $user->id to check if it was found in the database or not. This is because Eloquent populates all the attributes of the new model after it is saved.