Reputation: 4526
What is the difference between these methods:
find()
findOrFail()
first()
firstOrFail()
get()
list()
toArray()
I've been using them and each one gives a different result and sometimes I need to add toArray()
at the end of get()
because my function is expecting an array. Won't the other methods produce arrays as well?
Upvotes: 184
Views: 266019
Reputation: 1609
I know this is an older post, but I wanted to share my answer in case it helps anyone else.
find($id)
: Fetches a single model by primary key or returns null
.
$user = User::find(1);
findOrFail($id)
: Fetches a single model by primary key or throws an exception if not found.
$user = User::findOrFail(1);
first()
: Fetches the first record matching the query or returns null
.
$user = User::where('email', '[email protected]')->first();
firstOrFail()
: Fetches the first record matching the query or throws an exception if not found.
$user = User::where('email', '[email protected]')->firstOrFail();
get()
: Fetches all records matching the query constraints as a collection.
$users = User::where('status', '1')->get();
pluck($column)
: Fetches values of a single column as a collection.
$emails = User::pluck('email');
toArray()
: Converts the model or collection of models to an array.
$userArray = User::find(1)->toArray();
Upvotes: 2
Reputation: 1
There is also
$hsp = MyModel::firstWhere(['M_FK' => $x]);
Which returns the first model if found for a specific column
Upvotes: 0
Reputation: 71
Probably things changed but the findorFail method can take 2 arguments: $id
and $columns
mixed/array params respectively. Passing a second arg is not required. That said, this would work:
$post = Post::findOrFail([1,2], ['title', 'subtitle']);
If one of the $ids
fails, the ModelNotFoundException
with message 'No query results for model ... ' will be thrown.
Upvotes: 7
Reputation: 219938
find($id)
takes an id and returns a single model. If no matching model exist, it returns null
.
findOrFail($id)
takes an id and returns a single model. If no matching model exist, it throws an error1.
first()
returns the first record found in the database. If no matching model exist, it returns null
.
firstOrFail()
returns the first record found in the database. If no matching model exist, it throws an error1.
get()
returns a collection of models matching the query.
pluck($column)
returns a collection of just the values in the given column. In previous versions of Laravel this method was called lists
.
toArray()
converts the model/collection into a simple PHP array.
Note: a collection is a beefed up array. It functions similarly to an array, but has a lot of added functionality, as you can see in the docs.
Unfortunately, PHP doesn't let you use a collection object everywhere you can use an array. For example, using a collection in a foreach
loop is ok, put passing it to array_map
is not. Similarly, if you type-hint an argument as array
, PHP won't let you pass it a collection. Starting in PHP 7.1, there is the iterable
typehint, which can be used to accept both arrays and collections.
If you ever want to get a plain array from a collection, call its all()
method.
1 The error thrown by the findOrFail
and firstOrFail
methods is a ModelNotFoundException
. If you don't catch this exception yourself, Laravel will respond with a 404, which is what you want most of the time.
Upvotes: 426