雄辩收藏:计数和检测空 [英] Eloquent collection: counting and detect empty

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

问题描述

这可能是一个微不足道的问题,但是我想知道Laravel是否建议使用某种方式来检查是否从 $ result = Model :: where(...) - > get() 是空的,也是计数元素的数量。



我们正在使用!$ result 检测空的结果,是否足够?对于 count($ result),它实际上涵盖了所有的案例,包括空的结果?

解决方案

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



< $($ result){}
如果(!$ result){}
if($ result){}

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



要确定是否有任何结果,您可以执行任何以下内容:

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

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

  $ result = Model :: where(.. 。) - >首先(); 
if($ result){...}

注释/ / strong>





奖励信息



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




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

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?

解决方案

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

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

Because if you dd($result); you'll notice an instance of Illuminate\Support\Collection 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)) { }

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) { ... }

Notes / References

Bonus Information

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.

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

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