Eloquent Collection:计数和检测空 [英] Eloquent Collection: Counting and Detect Empty

查看:28
本文介绍了Eloquent Collection:计数和检测空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个微不足道的问题,但我想知道 Laravel 是否推荐某种方法来检查从 $result = Model::where(...)->get() 返回的 Eloquent 集合是否返回code>为空,同时计算元素个数.

This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well as counting the number of elements.

我们目前正在使用 !$result 来检测空结果,这样就足够了吗?至于count($result),是否真的涵盖了所有情况,包括空结果?

We are currently using !$result to detect empty result, is that sufficient? As for count($result), does it actually cover all cases, including empty result?

推荐答案

当使用 ->get() 时,你不能简单地使用以下任何一个:

When using ->get() you cannot simply use any of the below:

if (empty($result)) { }
if (!$result) { }
if ($result) { }

因为如果你 dd($result); 你会注意到 IlluminateSupportCollection 的实例总是被返回,即使没有结果.基本上你要检查的是 $a = new stdClass;if ($a) { ... } 将始终返回 true.

Because if you dd($result); you'll notice an instance of IlluminateSupportCollection is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... } which will always return true.

要确定是否有任何结果,您可以执行以下任一操作:

To determine if there are any results you can do any of the following:

if ($result->first()) { } 
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }

您也可以在查询构建器上使用 ->first() 而不是 ->get() ,这将返回第一个找到的模型的实例,或 null 否则.如果您只需要或期望从数据库中获得一个结果,这将非常有用.

You could also use ->first() instead of ->get() on the query builder which will return an instance of the first found model, or null otherwise. This is useful if you need or are expecting only one result from the database.

$result = Model::where(...)->first();
if ($result) { ... }

注释/参考

  • ->first() http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_first
  • isEmpty() http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_isEmpty
  • ->count() http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count
  • count($result) works because the Collection implements Countable and an internal count() method: http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count

Collection 和 Query Builder 的差异对于 Laravel 的新手来说可能有点混乱,因为两者的方法名称通常相同.出于这个原因,知道你在做什么可能会令人困惑.查询生成器本质上是构建一个查询,直到您调用一个方法,在该方法中它将执行查询并访问数据库(例如,当您调用某些方法时,例如 ->all() ->first() ->lists() 等).这些方法存在于Collection 对象中,如果有多个结果,可以从查询生成器返回.如果您不确定您实际使用的是哪个类,请尝试执行 var_dump(User::all()) 并尝试查看它实际返回的类(在 get_class 的帮助下)(...)).我强烈建议您查看 Collection 类的源代码,它非常简单.然后查看 Query Builder 并查看函数名称的相似之处,并找出它实际访问数据库的时间.

The Collection and the Query Builder differences can be a bit confusing to newcomers of Laravel because the method names are often the same between the two. For that reason it can be confusing to know what one you’re working on. The Query Builder essentially builds a query until you call a method where it will execute the query and hit the database (e.g. when you call certain methods like ->all() ->first() ->lists() and others). Those methods also exist on the Collection object, which can get returned from the Query Builder if there are multiple results. If you're not sure what class you're actually working with, try doing var_dump(User::all()) and experimenting to see what classes it's actually returning (with help of get_class(...)). I highly recommend you check out the source code for the Collection class, it's pretty simple. Then check out the Query Builder and see the similarities in function names and find out when it actually hits the database.

这篇关于Eloquent Collection:计数和检测空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆