support@codebucket.net

Laravel important methods: firstOrNew, firstOrCreate, firstOr, and updateOrCreate

Laravel important methods: firstOrNew, firstOrCreate, firstOr, and updateOrCreate

K. M. Shawkat Zamil | February 13, 2023

Hello Developers,

 

Today I will cover very important topics in Laravel. If you are familiar with the Laravel environment, you know the eloquent models like make(), create(), update(), and save(). Laravel also includes some other methods that are really useful for creating and updating models. Let's start.

 

 

Laravel: firstOrNew Method

 

 

The firstOrNew method is useful for finding the first model that matches some constraints or making a new one if there is not one that matches the constraints.

 

See the old format code:

 

$user = User::where('phone', request('phone'))->first();

if ($user === null) {
    $user = new User(['phone' => request('phone')]);
}

$user->name = request('name');

$user->save();

 

 

If we implement the firstOrNew method then the code looks like this:

 

 

$user = User::firstOrNew(['phone' =>  request('phone')]);
 
$user->name = request('name');
 
$user->save();

 

 

Laravel: firstOrCreate Method

 

 

The firstOrCreate method is very similar to the firstOrNew method. It tries to find a model matching the attributes you pass in the first parameter. If a model is not found, it automatically creates and saves a new model after applying the attributes passed as the second parameter.

 

 

$user = User::firstOrCreate(
    ['phone' =>  request('phone')],
    ['name' => request('name')]
);

// No call to $user->save() needed

 

 

Laravel: firstOr Method

 

 

The firstOr method is another interesting method. The firstOr method retrieves the first model from a query, or if no matches are found it will call a callback passed. This query is really helpful when you need to perform extra steps while creating a user or want different things rather than creating a new user.

 

 

$user = User::where('phone', request('phone'))->firstOr(function () {
    $account = Account::create([ //... ]);

    return User::create([
        'account_id' => $account->id,
        'email' => request('email'),
    ]);
});

 

 

Laravel: updateOrCreate Method

 

 

The updateOrCreate method is used to find model-matching constraints passed as the first parameters. 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 constraints passed as the first parameters and the attributes passed as the second parameters.

 

In general, we will write the below code:

 

$user = User::where('phone', request('phone'))->first();
 
if ($user !== null) {
    $user->update(['name' => request('name')]);
} else {
    $user = User::create([
      'phone' => request('phone'),
      'name' => request('name'),
    ]);
}

 

 

If we use the updateOrCreate method, the code looks like this:

 

 

$user = User::updateOrCreate(
    ['phone' =>  request('phone')],
    ['name' => request('name')]
);

// No call to $user->save() needed

 

 

I think these methods will help you in the journey of your development.

 

Read More: How to make Custom Forget Password Process in Laravel

 

 

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.