In Laravel, the updateOrCreate()
function is a convenient method that you can use to update an existing record or create a new one if it doesn’t exist. This method is useful when you want to update a record if it exists or create a new one if it doesn’t exist, without having to check if the record already exists yourself.
Here’s an example of how to use updateOrCreate()
function:
In this example, we are searching for a record with a id
field of “Existing post id”. If a record with that title exists, we update its name
field to “Updated post name”. If a record with that title doesn’t exist, we create a new record with the id
and body
fields set to “Existing post id” and “Updated post name”, respectively.

I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.
[…] https://www.devopsconsulting.in/blog/how-to-use-updateorcreate-function-laravel/ […]