Updated April 11, 2023
Definition on Laravel firstOrCreate
Laravel is used to build and customize web applications in the PHP framework which manages multiple things like authentication, template building, HTML scripting, routing, etc. It also helps the user by building his tasks which consume more manpower. In the firstorcreate method, the tool finds a matching model of the attribute which is passed as a first parameter. If the model or a suitable value is not found, it automatically develops and saves the new model after implementing any already passed attributes in the next parameter. 1$ user = user: firstorcreate. In this article, the implementation of the firstorcreate method is discussed briefly.
Overviews of Laravel firstorcreate
Laravel is an object-relational mapper which makes it interesting to communicate with the database. It works to retrieve records from the table in the database and also enables the user to update, insert, and delete records from the database or table. The firstorcreate method in Laravel is used to find the database record with the help of value pairs and given columns. If the user is unable to find the model in the database, the record will get inserted along with the attributes which result from appending the first array argument with the alternative argument in the second array.
How Laravel firstorcreate works?
If the user is working with Laravel most of the time, he should be aware of the standard methods in creating Laravel models like save(), update(), make(), create(). It has other few methods which are useful to create and update models which don’t require other steps. Apart from these standard models, there are other standards like firstornew, update, firstor, firstorcreate which are discussed in the below section.
FirstOrNew is used to find the first model which matches a few constraints or to make a new model if there are no other matches found from the constraints. The code to implement the firstornew method is below,
1
$user = User:: where ('email', request('email')) -> first();
2
if ($user === null)
{
$user = new User (['email' => request('email')]);
}
3
$user -> name = request ('name');
4
$user -> save()
This can also be used as
1
$user = User:: firstOrNew (['email' => request('email')]);
2
$user -> name = request ('name');
3
$user -> save ()
The user can also pass an array with additional attributes as a second parameter if no matching variables are found in the existing model.
$user = User:: firstOrNew
(
['email' => request('email')],
['name' => request('name')]
);
$user-> save();
firstOrCreate is used to match the attributes which are passed in the first parameter by the user. It is similar to the firstornew process. If the process doesn’t find any model, it creates and saves the new model automatically after implementing the attributes passed in the next parameter.
$user = User :: firstOrCreate
(
['email address' => request('email')],
['name of the model' => request('name')]
);
// no call is passed to $user-> save() required
firstOR
firstOr method is used at the time of source-driving and is used to retrieve the first model from the executed query or if any matching model is not found, it is used to make a call and a callback should be passed. It can be useful if the user has to execute additional steps when creating a new process or if the user wants to do some additional steps apart from creating a new user.
1
$user = User:: where ('email address', request('email')) -> firstOr (function () {
2
$account = Account:: create([ //... ]);
3
return User:: create ([
4
'account_id' => $ account -> id,
5
'email' => request ('email'),
6
]);
7
});
UpdateOrCreate is used to find the matching model of the constraints passed like the first parameter. If the user finds the matching model, he will update the matching attributes which is passed as the next parameter. In the new model, if no matching data were found, it is created in both the constraints passed as the first argument and the parameters passed in the next or second argument.
The user can use the code to implement the UpdateOrCreate function.
1
$user = User:: where ('email', request ('email'))-> first();
2
if ($user !== null)
{
3
$user -> update (['name' => request('name')]);
}
4
else {
5
$user = User:: create([
6
'email' => request ('email'),
7
'name' => request ('name'),
8
]);
9
}
10
// editing can be done further.
The simple syntax for updateorcreate method is below,
1
$ user = user:: updateorcreate()
(
'email' => request ('email')],
['name' => request ('name')]
);
Laravel firstorcreate Model Method
when working on an application, the user has to search a table for instance and if it doesn’t exist he can create a new one and save it in the table. similar to firstorcreate(), there is another option firstornew which retains the first instance if it exists or is created as a new instance without saving it in the actual table.
Firstorcreate is used to locate the first model which matches the suitable constraints or build a new instance if any values don’t have the matching constraints. It fetches the matching of the first record and attributes or it creates a new suitable parameter to fit in the table.
Then it searches for the model with a suitable parameter which is passed as the first parameter.
If no matching model is found, it develops and saves it as a new model after implementing the value of the key-pair passed like the second parameter.
$ uuid = request ('uuid');
$device = Device:: where ('uuid', $uuid)->first();
if ($device === null)
{
$device = new Device (['uuid' => $uuid]);
}
$device-> operating_system = request ('operatingSystem');
$device-> version = request ('version');
$device-> save();
Conclusion
The Laravel used to provide an instant option which firstorcreate() which performs both functions like creating new and searching and also return the instances. So the user can prefer this model as a simple choice.
Recommended Articles
We hope that this EDUCBA information on “Laravel firstOrCreate” was beneficial to you. You can view EDUCBA’s recommended articles for more information.