Laravel first or new

In Laravel, first() and new() are two different methods that serve distinct purposes.

first():

first() is a method on Eloquent query builders that returns the first record that matches the query. It's often used to retrieve a single record from a database table. For example:

$user = User::where('email', '[email protected]')->first();

This code will retrieve the first user record with the email [email protected] and assign it to the $user variable.

new():

new() is a PHP constructor that creates a new instance of a class. In Laravel, you can use new to create a new instance of an Eloquent model, such as a User model:

$user = new User();

This code will create a new, empty User model instance and assign it to the $user variable.

When to use each:

In summary: