176

What is the difference between these methods:

  1. find()
  2. findOrFail()
  3. first()
  4. firstOrFail()
  5. get()
  6. list()
  7. 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?

2
  • 1
    Have a look at the Query Builder docs, as well as the Eloquent API reference. Oct 8, 2015 at 22:57
  • All information from @Joseph Silber is correct and very useful. I want to add an answer to list() From Laravel 5.2 The list method on the Collection, query builder and Eloquent query builder objects have been renamed to pluck. The method signature remains the same.
    – Nirav Bhoi
    Oct 31, 2018 at 5:47

2 Answers 2

412
  1. find($id) takes an id and returns a single model. If no matching model exist, it returns null.

  2. findOrFail($id) takes an id and returns a single model. If no matching model exist, it throws an error1.

  3. first() returns the first record found in the database. If no matching model exist, it returns null.

  4. firstOrFail() returns the first record found in the database. If no matching model exist, it throws an error1.

  5. get() returns a collection of models matching the query.

  6. pluck($column) returns a collection of just the values in the given column. In previous versions of Laravel this method was called lists.

  7. 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.

7
  • 1
    Thank you for the clear up. Here's the thing, I am trying to get a list of posts like this $post = Post::with('user.votes')->with('subreddit.moderators')->findOrFail($post->id); so I can access $post->subreddit->moderators->user_id but that is giving Undefined property: Illuminate\Database\Eloquent\Collection::$user_id - I can access $post->subreddit->moderators but not when I added user_id - why is that?
    – Halnex
    Oct 8, 2015 at 23:25
  • subreddit is a single model, with a moderators property. moderators is a collection, not a single model. You can't access anything directly off of it. You can check the first one if you want: $post->subreddit->moderators[0]->user_id Oct 8, 2015 at 23:36
  • I think you should replace "it throws an error" with "it throws an ModelNotFoundException exception" :D Anyway great contribution ;) Mar 21, 2018 at 7:42
  • 1
    You mean "instance", not "model". A model is a conceptual structure, or an example, not an actual instance. Let's be clear in our terminology. Sadly in the Eloquent world people are using the two terms interchangeably. An instance is not a model. An instance is a real implementation (object version) of a model.
    – GeneC
    Dec 27, 2020 at 21:01
  • 1
    @GeneC - The Laravel documentation calls instances models: "throw an exception if a model is not found" Dec 28, 2020 at 1:30
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.