What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()

PhpLaravelLaravel 5

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

  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.

Solution 2 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHalnexView Question on Stackoverflow
Solution 1 - PhpJoseph SilberView Answer on Stackoverflow
Solution 2 - PhpGrogensteinView Answer on Stackoverflow