If you are using the laravel, you probably familiar with the first
, find
, create
, update
and save
methods.
There are some other methods too like firstOrNew
, firstOrCreate
, firstOr
, and updateOrCreate
that will not only save your time but also improves the code readability and simplicity.
Let me explain all of those, so lets get started!
Table of Contents
1- firstOrNew
The firstOrNew
method is good in the case when you are finding first row that matches with some condition. If not found, it will create model with the details but not save it into the database.
Here is an example of code that looks like this without firstOrNew
:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::where('email', $user_email)->first();
if ($user === null) {
$user = new User(['email' => $user_email]);
}
$user->name = $user_name;
$user->save();
Here is an example of code using firstOrNew
Method:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::firstOrNew(
['email' => $user_email],
['name' => $user_name]
);
$user->save();
The above code of firstOrNew
take the first argument array and search in the database.
If found, return the first found row.
If not found, then it will create the new model row with the first and second arguments. Note we have to save it in order to get the row created in the database.
2- firstOrCreate
Similar to firstOrNew
, firstOrCreate
the method is useful in the case where you want to find the first row that matches some condition, otherwise want to create and save into the database.
The only difference between firstOrNew
and firstOrCreate
is that firstOrNew
method doesn’t the save into the database, but it only creates the model row. We have so manually using save
method.
Here is an example of code that looks like this without firstOrCreate
:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::where('email', $user_email)->first();
if ($user === null) {
$user = new User(['email' => $user_email]);
}
$user->name = $user_name;
$user->save();
Here is an example of code using firstOrCreate
:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::firstOrCreate(
['email' => $user_email],
['name' => $user_name]
);
The above code of firstOrCreate
method, takes two arguments as the array, it will take the first argument array and checks in the database.
If exists, it will return that row.
If not exists, it will create the row using first and second argument arrays, and return that row.
3- firstOr
The firstOr
method is useful in the case of when you want to find the first row with some conditions and if not found, it returns the callback function, so that you can perform extra complicated steps.
Here is an example of the code using firstOr
method:
$user_email=$request->get('email');
$user = User::where('email', $user_email)->firstOr(function ($user_email) {
$new_account= Account::create([ 'email'=>$user_email ]);
return User::create([
'account_id' => $new_account->id,
'email' => $user_email,
]);
})
In the above code, first we are looking for the user email.
If found, the $user
variable will have that user object.
If not found, it will go to the callback function, in which we can do whatever we want. In the above code, first we are creating the email in other table and then we are creating the user
and returning it.
4- updateOrCreate
The updateOrCreate
method is useful in the case where you want to update the existing row. If not found you want to create the new row.
Here is an example of the code without updateOrCreate
:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::where('email', $user_email)->first();
if ($user !== null) {
$user->update(['name' => $user_name]);
} else {
$user = User::create([
'email' => $user_email,
'name' => $user_name,
]);
}
And here is the example code which us using updateOrCreate
:
$user_email=$request->get('email');
$user_name=$request->get('name');
$user = User::updateOrCreate(
['email' => $user_email],
['name' => $user_name]
);
In the above code, the updateOrCreate
method accepts two arguments as an array. It will use the first array and search the database.
If found, it will update the columns which are in the second argument and return that updated row.
If not found, it will create a new row using the first and second arguments and return it.
Conclusion
In the above code, we have discussed firstOrNew
, firstOrCreate
, firstOr
, and updateOrCreate
Laravel eloquent methods.
Let me know if you have any questions. See you in the next one 🙂
Write a Reply or Comment