雄辩计数()总是返回1 [英] Eloquent count() always returns 1
本文介绍了雄辩计数()总是返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下查询:
Item :: select(['items.id','inventory.quantity' ]
- > leftJoin('inventory','items.id','=','inventory.item_id')
- > groupBy('items.id')
- >计数();
count()
方法总是返回1尽管返回的结果有20行。为什么会这样?
这是来自 DB :: getQueryLog()
的原始查询:
从
`items`
中选择
计数(*)作为汇总
$ join
`inventory` ON`items`.`id` =`inventory`.`item_id`
group by`items`.`id`
解决方案
是的, count
只返回1行, >
您可能想要:
Item :: select(['items。 id作为id','inventory.quantity as quantity'])
- > leftJoin('inventory','items.id','=','inventory.item_id')
- > groupBy('items.id')
- > lists('quantity','id');
这将返回一个包含 id
的数组键和数量
作为值。否则使用获取
,但是如果您想要分组的结果,则从不计数
。
I have the following query:
Item::select(['items.id', 'inventory.quantity'])
->leftJoin('inventory', 'items.id', '=', 'inventory.item_id')
->groupBy('items.id')
->count();
The count()
method always returns 1 despite there being 20 rows to the results that are returned. Why might this be?
Here is the raw query from DB::getQueryLog()
:
select
count(*) as aggregate
from
`items`
left join
`inventory` ON `items`.`id` = `inventory`.`item_id`
group by `items`.`id`
解决方案
Yes, count
returns only 1 row, always.
You would probably want:
Item::select(['items.id as id', 'inventory.quantity as quantity'])
->leftJoin('inventory', 'items.id', '=', 'inventory.item_id')
->groupBy('items.id')
->lists('quantity', 'id');
this will return an array with id
as keys, and quantity
as values. Otherwise use get
, but never count
if you want grouped results.
这篇关于雄辩计数()总是返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文