Laravel

Laravel firstOrNew, firstOrCreate, firstOr, updateOrCreate With Example

in this article, we’ll discuss some useful methods of Laravel eloquent model class. The methods are: firstOrNew(), firstOrCreate(), firstOr(), and updateOrCreate(). We’ll discuss some useful tips and examples of each of them.

Larave Elequonte Method

Let’s explore four essential Eloquent methods: firstOrNew, firstOrCreate, firstOr, and updateOrCreate with example.

firstOrNew : Retrieve Or Create a New

The firstOrNew() method is used to get the first record that matches the specific conditions or instantiate a new model instance if no matching record is found.

// Example using firstOrNew
$emp = Employee::where('email', 'adam@example.com')->firstOrNew(['name' => 'Adam Joe']);
$emp->save();

in the above code, the Employee model is queried for a record with the specified email. If found, it retrieves the existing employee; otherwise, it creates a new employee with the provided data and saves it to the database.

firstOrCreate

The firstOrCreate method retrieves the first matching record but also creates a new record in the database with the specified attributes if no match is found. It’s similar to firstOrNew() except firstOrCreate() automatically saves the newly created record to the database.

// Example using firstOrNew
$emp = Employee::firstOrCreate(
['email', 'adam@example.com'], // Conditions for finding or creating
['name' => 'Adam Joe'] // Additional attributes for creating if not found
);

In this example:

The first parameter of firstOrCreate is an array specifying the conditions to search for an existing record.
The second parameter is an array of attributes to use when creating a new record if no match is found.

Related Post

You can use additional conditions in combination with firstOrCreate by using the where method.

// Example using firstOrNew
$emp = Employee::firstOrCreate(
['email', 'adam@example.com'], // Conditions for finding or creating
['name' => 'Adam Joe'] // Additional attributes for creating if not found
)->where('active', true);

firstOr Method

The firstOr method is used to get the first result of a query or return a default value if no result is found. You can also handle cases where you want to retrieve a record and provide a default value if that record doesn’t exist.

$emp = Employee::where('name', 'Adam')->firstOr(['name' => 'Adam']);

in the above example, We have retrieved an employee with the name ‘Adam’, if no such employee exists in the database then create a new Employee instance with the default attributes.

The firstOr method accepts an array that represents the default values to be used when no result is found.

updateOrCreate Method

The updateOrCreate method combines the functionality of updating existing records or creating new records based on specified conditions. This method is useful when you want to update a record if it exists or create a new one if it doesn’t.

// Example using updateOrCreate
$emp = Employee::updateOrCreate(
['email' => 'adam@example.com'],
['name' => 'Adam', 'age' => 30]
);

In this example, if a user with the email ‘adam@example.com’ exists, it updates the existing user’s name and age. If not, it creates a new user with the specified attributes email, name, and age.

Conclusion

The Laravel has an inbuilt Eloquent ORM with powerful methods: such as firstOrNew, firstOrCreate, firstOr, and updateOrCreate. These methods provide concise and expressive solutions for retrieving records, creating new ones, and updating existing ones. You can use it as per your project requirements.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories