php find()、findOrFail()、first()、firstOrFail()、get()、list()、toArray()有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33027047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:20:13  来源:igfitidea点击:

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

phplaravellaravel-5

提问by Halnex

What is the difference between these methods:

这些方法有什么区别:

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

我一直在使用它们,每个都给出了不同的结果,有时我需要toArray()在末尾添加,get()因为我的函数需要一个数组。其他方法也不会产生数组吗?

回答by Joseph Silber

  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.

  1. find($id)接受一个 id 并返回一个模型。如果不存在匹配的模型,则返回null

  2. findOrFail($id)接受一个 id 并返回一个模型。如果不存在匹配模型,则会抛出错误1

  3. first()返回在数据库中找到的第一条记录。如果不存在匹配的模型,则返回null

  4. firstOrFail()返回在数据库中找到的第一条记录。如果不存在匹配模型,则会抛出错误1

  5. get()返回与查询匹配的模型集合。

  6. pluck($column)仅返回给定列中值的集合。在之前的 Laravel 版本中,这个方法被称为lists.

  7. toArray()将模型/集合转换为一个简单的 PHP 数组。



Note:a collectionis a beefed up array. It functions similarly to an array, but has a lot of added functionality, as you can see in the docs.

注意:集合是一个增强的数组。它的功能类似于数组,但具有许多附加功能,如您在docs 中所见。

Unfortunately, PHP doesn't let you use a collection object everywhere you can use an array. For example, using a collection in a foreachloop is ok, put passing it to array_mapis 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 iterabletypehint, which can be used to accept both arrays and collections.

不幸的是,PHP 不允许您在可以使用数组的任何地方使用集合对象。例如,在foreach循环中使用集合是可以的,将它传递给array_map则不行。类似地,如果您将参数类型提示为array,PHP 不会让您将集合传递给它。从 PHP 7.1 开始,有iterabletypehint,可用于接受数组和集合。

If you ever want to get a plain array from a collection, call its all()method.

如果您想从集合中获取普通数组,请调用其all()方法。



1The error thrown by the findOrFailand firstOrFailmethods 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.

1findOrFailfirstOrFail方法抛出的错误是ModelNotFoundException. 如果你自己没有捕捉到这个异常,Laravel 会以 404 响应,这是你大多数时候想要的。

回答by Nirav Bhoi

All information from @Joseph Silber is correct and very useful.

@Joseph Silber 提供的所有信息都是正确且非常有用的。

I want to add an answer to list()

我想为 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.

从 Laravel 5.2 开始,集合、查询构建器和 Eloquent 查询构建器对象上的列表方法已重命名为 pluck。方法签名保持不变。