4
In certain part of my application written in Laravel I have this consultation:
$counts = User::select(\DB::raw('count(*) as status_count, status'))
->groupBy('status')
->get();
It returns me at least 1 record and at most 3, these are exactly the existing status. How do I return an extra line containing the total of records?
Thanks friend, however, I was trying to avoid an extra consultation. For this I used Rafael’s suggestion
– Fábio Jânio
I edited the answer. Actually his solution is good, but you’d be remaking something Laravel has already done internally. The method
Collection::sum
already makes aCollection::reduce
internally.– Wallace Maxters
Nice. Thank you very much
– Fábio Jânio