Find or Create with Eloquent

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Simplifying Model Creation and Retrieval with Find or Create in Laravel Eloquent Introduction: As a Laravel developer, you might have experienced the need to find or create models efficiently. In this blog post, we will explore the concept of find or create using Laravel's Eloquent ORM. We will provide real-life examples and best practices for using these techniques effectively in your applications. 1. Understanding Model Operations: Before diving into the find or create functionality, let's quickly review the basics of model operations. When working with models within Laravel Eloquent, there are three main methods to handle data persistence. You can find a record using the 'find()' method, create a new record with the 'create()' method, and update or delete an existing record by calling its corresponding methods. 2. Find or Create: The Solution The 'findOrCreate()' method is a powerful utility that combines both finding and creating operations in one simple command. It first attempts to find the model based on your criteria using the 'where()' method. If no matching record is found, it will then proceed with creating a new instance of the model and save it to the database. 3. Implementing Find or Create: Examples Here are a couple of examples that demonstrate the usage of 'findOrCreate()'. In our case, we assume there's an existing User model in your Laravel application. Example 1 (Update Existing Record): Assuming you want to update the user with ID 123 and set its age to 25:
$user = User::findOrCreate(
    ['id' => 123],
    [
        'age' => 25,
    ]
);
In this scenario, the method first checks if a user with ID 123 exists. If it does, an update query is executed to set the age field to 25. If not, a new user with the given details will be created, and its instance will be returned. Example 2 (Create New Record): To create a new user if one doesn't already exist with the same email address:
$user = User::findOrCreate(
    [
        'email' => 'john@example.com',
    ],
    [
        'first_name' => 'John',
        'age' => 30,
    ]
);
Here, the above code creates a new user if no user already exists with the email address 'john@example.com'. It will set both its first name and age fields to the given values while creating it. 4. Best Practices: To get the most out of find or create operations, follow these best practices: - Use the 'findOrCreate()' method over multiple if-else statements for better readability and maintainability. - Be mindful of the conditionals in the first argument list (e.g., ['id' => 123] vs []) to ensure that only relevant models are found or created during the operation. - Keep the second argument list as concise as possible, avoiding unnecessary or duplicate data. - Use appropriate column names and values for efficient database operations. Conclusion: Incorporating find or create functionality into your Laravel applications can lead to a more streamlined development experience. By understanding these techniques, you can simplify various complex scenarios in your codebase while maintaining readability and maintainability. Feel free to explore the Eloquent documentation for further details on working with models, or check out our blog post series on Laravel tips and tricks for expert-level guidance.