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', '[email protected]')->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', '[email protected]'], // 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.
You can use additional conditions in combination with firstOrCreate by using the where method.
// Example using firstOrNew $emp = Employee::firstOrCreate( ['email', '[email protected]'], // 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' => '[email protected]'], ['name' => 'Adam', 'age' => 30] );
In this example, if a user with the email ‘[email protected]’ 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.