Laravel Eloquent provides two methods for finding or creating records: firstOrNew() and firstOrCreate(). The main difference is saving the new model to the database.

firstOrCreate(): If the record doesn’t exist, Laravel creates and automatically saves it to the database.

firstOrNew(): If the record doesn’t exist, Laravel creates a new model instance but doesn’t save it. You must call save() manually.

2 exmaples for firstOrCreate:

find or create a user:

PHP
$user = User::firstOrCreate(
      ['email' => 'john@example.com'],
      ['name' => 'John']
);

f the user doesn’t exist, Laravel creates and saves it automatically.

Find or create a category:

PHP
$category = Category::firstOrCreate([
       'name' => 'Programming'
]);

No need to call save().

2 examples for firstOrNew:

Find or create a user:

PHP
$user = User::firstOrNew(
      ['email' => 'john@example.com'],
      ['name' => 'John']
);

If the user doesn’t exist, Laravel creates a new model instance without saving it. To save it you should call $user->save()

Modify a model before saving:

PHP
$user = User::firstOrNew(
    ['email' => 'john@example.com']
);
$user->name = 'John';
$user->status = 'active';
$user->save();

You can modify the model before inserting it into the database.

# When Should You Use Them?

Use firstOrCreate() when you want to find a record or immediately create it if it doesn’t exist.

Use firstOrNew() when you need to modify, validate, or prepare a new model before saving it.

In short: firstOrCreate() automatically saves new records, while firstOrNew() gives you control over when to save them.