这可能是一个微不足道的问题,但我想知道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查询生成器

其他回答

你可以使用:$counter = count($data);

当使用->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->isEmpty()

这与empty($result)不同,后者不会为真,因为结果将是一个空集合。您建议的count($result)也是一个很好的解决方案。我在文件里找不到任何参考资料

我觉得还是用旧的比较好

$result->isEmpty();

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

我觉得你可以试试

  @if(!$result->isEmpty())
         // $result is not empty
    @else
        // $result is empty
    @endif

或者也可以用

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