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

我们目前正在使用!$result来检测空结果,这足够了吗?至于count($result),它实际上涵盖了所有情况,包括空结果吗?


当前回答

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

if (empty($result)) { }
if (!$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(),后者将返回第一个找到的模型的实例,否则为空。如果您只需要或期望从数据库中得到一个结果,这是非常有用的。

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

备注/参考资料

- >第()http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_first http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_isEmpty isEmpty () - >数()http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_count count($result)有效,因为Collection实现了Countable和一个内部count()方法:http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count

额外的信息

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.

Laravel 5.2集合类 Laravel 5.2查询生成器

其他回答

我同意以上认可的答案。但通常我使用$results->isNotEmpty()方法如下所示。

if($results->isNotEmpty())
{
//do something
}

它比if(!results->isEmpty())更冗长,因为有时我们会忘记添加'!',这可能会导致不必要的错误。

注意,这个方法从5.3版本开始就存在了。

所以Laravel实际上是在使用Model::all()时返回一个集合; 你不想要一个集合,你想要一个数组,所以你可以类型设置它。 (数组)模型:所有();然后可以使用array_filter返回结果

$models = (array)Model::all()
$models = array_filter($models);
if(empty($models))
{
 do something
}

这也将允许您执行count()之类的操作。

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

if (empty($result)) { }
if (!$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(),后者将返回第一个找到的模型的实例,否则为空。如果您只需要或期望从数据库中得到一个结果,这是非常有用的。

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

备注/参考资料

- >第()http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_first http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_isEmpty isEmpty () - >数()http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html method_count count($result)有效,因为Collection实现了Countable和一个内部count()方法:http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count

额外的信息

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.

Laravel 5.2集合类 Laravel 5.2查询生成器

你可以这样做

$result = Model::where(...)->count(); 

计算结果。

你也可以使用

if ($result->isEmpty()){}

检查结果是否为空。

根据Laravel文档,你可以这样使用:

$result->isEmpty();

如果集合为空,isEmpty方法返回true;否则返回false。