What is the difference between find(), findOrFail(

2019-01-12 23:28发布

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条回答
一夜七次
2楼-- · 2019-01-12 23:54
  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. Newer versions of PHP have 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.

查看更多
Emotional °昔
3楼-- · 2019-01-13 00:00

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.

查看更多
登录 后发表回答