Laravel雄辩groupBy()还返回每个组的计数 [英] Laravel Eloquent groupBy() AND also return count of each group
本文介绍了Laravel雄辩groupBy()还返回每个组的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,其中包含一列浏览器版本。而我只想从记录集中知道每种类型的浏览器有多少。所以,我需要这样的结果:总记录:10; Internet Explorer 8:2;铬25:4; Firefox 20:4.(全部加起来10)
I have a table that contains, amongst other columns, a column of browser versions. And I simply want to know from the record-set, how many of each type of browser there are. So, I need to end up with something like this: Total Records: 10; Internet Explorer 8: 2; Chrome 25: 4; Firefox 20: 4. (All adding up to 10)
这是我的两便士:
$user_info = Usermeta::groupBy('browser')->get();
当然,只包含3个浏览器,而不是每个浏览器的数量。如何做到这一点?
Of course that just contains the 3 browsers and not the number of each. How can I do this?
推荐答案
这对我有用:
$user_info = DB::table('usermetas')
->select('browser', DB::raw('count(*) as total'))
->groupBy('browser')
->get();
这篇关于Laravel雄辩groupBy()还返回每个组的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文