Home > PHP Framework > Laravel > body text

firstOrNew, firstOrCreate, firstOr and updateOrCreate methods in laravel

藏色散人
Release: 2020-05-16 11:54:05
forward
6058 people have browsed it

The following is developed by LaravelThe 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!

firstOrNew, firstOrCreate, firstOr and updateOrCreate methods in laravel

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()
Copy after login

and rewrite it as:

$user = User::firstOrNew(['email' =>  request('email')]);
$user->name = request('name');
$user->save()
Copy after login

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();
Copy after login

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
Copy after login

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'), 
    ]);
});
Copy after login

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
Copy after login

Use the updateOrCreate method:

$user = User::updateOrCreate(
    ['email' =>  request('email')],
    ['name' => request('name')]
);
// Do other things with the User
Copy after login

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 visit laraveltutorial!

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!