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:
this will return an array with
id
as keys, andquantity
as values. Otherwise useget
, but nevercount
if you want grouped results.If you only need the correct number of rows from a grouped result, and you don't care that much about the performance then you can call
get()
first and then callcount()
on that.