Laravel Eloquent Methods firstOrNew firstOrCreate updateOrCreate

Laravel Eloquent Methods firstOrNew firstOrCreate updateOrCreate

In this tutorial, we will learn laravel eloquent methods firstOrNew(), firstOrCreate() and updateOrCreate(). These methods are very helpful but most users are not familiar with them and don’t know how to use them. These are available in laravel 6, laravel 7, and laravel 8 version.

You mostly used the standard function to create the models and saving the data in the database. Those standard methods are save(), create(), delete(), update(). Except for these one laravel have more advanced functions to create, delete, update data while checking the data is exists or not.

Check for more details about these functions here

So let’s see those advanced methods are following.

  • firstOrNew()
  • firstOrCreate()
  • updateOrCreate()

Let’s see how they might be useful for you.

firstOrNew()

The firstOrNew() method is used to find the first Model by matching the condition and make a new record with the calling save() function if the Model does not exist. It is really useful to check and add new records together. It will check first then it will insert a new one.

Let’s understand with the example code.

Normally when you add new data with conditions, you probably make two conditions. The first one is for checking and the second one is for inserting based on the check condition. See the code below.

In the above, it will get the first Task Model with specify client_id and then check data exist or not then insert new data to Task Model.

What if we do this same with firstOrNew() methods?

Let’s see!

Did you see the magic? You just check with just one line using firstOrNew() eloquent method. I’m sure you will love this method.

Note: You can also pass the multiple parameters to check the Model is exist or not and then insert a new one.

firstOrCreate()

The firstOrCreate() method checks for all parameters to existing before finding the match. If all parameters do not match then it will create new Model data.

It is similar to firstOrNew() but the difference is that:

firstOrNew(): As discussed above, it will save your Model if the Model does not exist but it gives you a Model instance and you need to call the save() function.
firstOrCreate(): In this method, you don’t need to call save() function. It will automatically save the Model if does not exist.

See the code example.

Note: No need to call save() function.

updateOrCreate()

The updateOrCreate() method is used to find the matching condition using passed as the first parameter and then update the Model with passing attributes value if matching Model found.

But If it does not found then it will create the new Model data using that passing attribute.

When you update normally then you do the same as we did for firstOrNew() methods. First, check the data that exist with the specific condition using the where() function and then update the record using the update() function.

Let’s see the code below!

And let’s see same example with updateOrCreate() function.

I hope you understand the powerful laravel eloquent methods ( firstOrNew(), firstOrCreate() and updateOrCreate() ) that will you to write a clean code and improve your laravel skill.

If you have any questions please let me know in the comment section, I’ll respond to you as soon as possible.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *