Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 } 
like image 296
sigmaxf Avatar asked Jun 06 '15 19:06

sigmaxf


People also ask

What does First () return in laravel?

It just returns null.

What is get and first in laravel?

get method give a collection and first method give you a model instance. When you want to get collection of data means, lots of results then use get but if you want to retrieve only one result then use first.

How do I skip the first record in laravel?

skip() will help to skip some records when you fetch data from the database table. So, let's see bellow examples that will help you how to use take() and skip() eloquent query in laravel.

What does latest do in laravel?

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.


2 Answers

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. } 
like image 114
patricus Avatar answered Sep 17 '22 17:09

patricus


You can check if your user was recently created.

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

(Source: Thomas Kim from this answer)

like image 32
mhellmeier Avatar answered Sep 21 '22 17:09

mhellmeier