Home>Article>PHP Framework> firstOrNew, firstOrCreate, firstOr and updateOrCreate methods in laravel
The following is developed byLaravelThe introductory tutorial column will introduce you to the firstOrNew, firstOrCreate, firstOr and updateOrCreate methods in laravel. I hope it will be helpful to friends in need!
If you have ever used Laravel, you probably know the standard ways to create Eloquent models, such as make(), create(), update, and save(). Laravel also provides some other methods that people don't realize are also very useful for creating and updating models. Therefore, in this article I would like to introduce some other methods and show that they may be useful:
firstOrNew
firstOrNew method finds the first one that satisfies certain constraints Model, new creates a new model when there is no data that satisfies the constraints.
You can use the following code:
$user = User::where('email', request('email'))->first(); if ($user === null) { $user = new User(['email' => request('email')]); } $user->name = request('name'); $user->save()
and rewrite it as:
$user = User::firstOrNew(['email' => request('email')]); $user->name = request('name'); $user->save()
If you cannot find an existing model, you can also pass it through the second parameter An array of additional properties:
$user = User::firstOrNew( ['email' => request('email')], ['name' => request('name')] ); $user->save();
firstOrCreate
The firstOrCreate method is very similar to the firstOrNew method. It will try to find a matching model based on the first parameter you pass. If it is not found, it will automatically create and save a new model using the value passed in the second parameter:
$user = User::firstOrCreate( ['email' => request('email')], ['name' => request('name')] ); // No call to $user->save() needed
firstOr
I recently discovered the firstOr method when I was fishing. The firstOr method will retrieve the first piece of data. If no matching data is found, the incoming callback will be executed. This is useful if you need to take extra steps when creating a user, or want to do something other than create a new user:
$user = User::where('email', request('email'))->firstOr(function () { $account = Account::create([ //... ]); return User::create([ 'account_id' => $account->id, 'email' => request('email'), ]); });
updateOrCreate
The updateOrCreate method attempts to find a model that matches the constraints passed in the first argument. If a matching model is found, it will update the model with the properties passed in the second parameter. If a matching model is not found, a new model will be created, passing in the first and second arguments.
You can refactor this code:
$user = User::where('email', request('email'))->first(); if ($user !== null) { $user->update(['name' => request('name')]); } else { $user = User::create([ 'email' => request('email'), 'name' => request('name'), ]); } // Do other things with the User
Use the updateOrCreate method:
$user = User::updateOrCreate( ['email' => request('email')], ['name' => request('name')] ); // Do other things with the User
Conclusion
In summary, I think these methods can help you simplify your code in some cases! Do you know of any other useful little-known tips? If so, please let me know. I love learning about the little details that make Laravel so great.
For more laravel framework technical articles, please visitlaraveltutorial!
The above is the detailed content of firstOrNew, firstOrCreate, firstOr and updateOrCreate methods in laravel. For more information, please follow other related articles on the PHP Chinese website!