Different Laravel methods firstOrNew, firstOrCreate, firstOr, and updateOrCreate

Standard methods for creating Eloquent Models like make()create()update, and save(). Laravel includes some other methods are that also really useful for creating and updating Models that I feel don’t get enough attention. So in this article, I’d like to go over some of these additional methods and explain how they might be useful.

1. firstOrNew

The firstOrNew method is really useful for finding the first Model that matches some constraints or making a new one if there isn’t one that matches those constraints.

You can take a piece of code that looks like this:

$user = User::where(‘email,’$request->email)->first();

if($user === null){

$user = new User([ ‘email’ => $request->email ]);

$user->name = $request->name;

$user->save();

}

And turn it into this:

$user = User::firstOrNew( [ ‘email’ => $request->email ] );

$user->name = $request->name;

$user->save();

You may also pass an array of additional attributes to set as the second parameter if no existing Model is found:

$user = User::firstOrNew([‘email’ => request(‘email’)],[‘name’ => request(‘name’)]);

$user->save();

2.firstOrCreate

I recently found the firstOr method while source-diving. The firstOr method retrieves the first Model from a query, or if no matching Model is found, it will call a callback passed. This can be really useful if you need to perform extra steps when creating a user or want to do something other than creating a new user:

$user = User::where(‘email,’$request->email)->firstOr(function(){

$account = Account::create([

return User::create([

‘account_id’ => $account->id,

‘email’ => $request->email

]);

]);

});

3.firstOr

I recently found the firstOr method while source-diving. The firstOr method retrieves the first Model from a query, or if no matching Model is found, it will call a callback passed. This can be really useful if you need to perform extra steps when creating a user or want to do something other than creating a new user:

$user = User::where(‘email,’$request->email)->firstOr(function(){

$account = Account::create([

return User::create([

‘account_id’ => $account->id,

‘email’ => $request->email

]);

]);

});

4.updateOrCreate

The updateOrCreate method attempts to find a Model matching the constraints passed as the first parameter. If a matching Model is found, it will update the match with the attributes passed as the second parameter. If no matching Model is found a new Model will be created with both the constraints passed as the first parameter and the attributes passed as the second parameter.

You can refactor this piece of 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

]);

}

To this using the updateOrCreate method:

$user = User::updateOrCreate([

‘email’ => $request->email,

‘name’ => $request->name

]);

Tsuki



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム