Laravel Eloquent groupBy() 并且还返回每个组的计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18533080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:37:20  来源:igfitidea点击:

Laravel Eloquent groupBy() AND also return count of each group

laravellaravel-4eloquent

提问by kJamesy

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)

我有一个表格,其中包含一列浏览器版本。我只是想从记录集中知道每种类型的浏览器有多少。所以,我需要得到这样的结果:Total Records: 10; Internet Explorer 8:2;铬 25:4;Firefox 20:4。(全部加起来为 10)

Here's my two pence:

这是我的两便士:

$user_info = Usermeta::groupBy('browser')->get();

Of course that just contains the 3 browsers and not the number of each. How can I do this?

当然,这只包含 3 个浏览器,而不是每个浏览器的数量。我怎样才能做到这一点?

回答by Antonio Carlos Ribeiro

This is working for me:

这对我有用:

$user_info = DB::table('usermetas')
                 ->select('browser', DB::raw('count(*) as total'))
                 ->groupBy('browser')
                 ->get();

回答by carlituxman

This works for me (Laravel 5.1):

这对我有用(Laravel 5.1):

$user_info = Usermeta::groupBy('browser')->select('browser', DB::raw('count(*) as total'))->get();

回答by Diogo Gomes

Thanks Antonio,

谢谢安东尼奥,

I've just added the listscommand at the end so it will only return one array with key and count:

我刚刚lists在最后添加了命令,因此它只会返回一个带有键和计数的数组:

Laravel 4

Laravel 4

$user_info = DB::table('usermetas')
             ->select('browser', DB::raw('count(*) as total'))
             ->groupBy('browser')
             ->lists('total','browser');

Laravel 5.1

Laravel 5.1

$user_info = DB::table('usermetas')
             ->select('browser', DB::raw('count(*) as total'))
             ->groupBy('browser')
             ->lists('total','browser')->all();

Laravel 5.2+

Laravel 5.2+

$user_info = DB::table('usermetas')
             ->select('browser', DB::raw('count(*) as total'))
             ->groupBy('browser')
             ->pluck('total','browser')->all();

回答by Adam Kozlowski

If you want to get collection, groupBy and count:

如果要获取集合、groupBy 和计数:

$collection = ModelName::groupBy('group_id')
->selectRaw('count(*) as total, group_id')
->get();

Cheers!

干杯!

回答by Yauheni Prakopchyk

Works that way as well, a bit more tidy. getQuery()just returns the underlying builder, which already contains the table reference.

也可以这样工作,更整洁一些。 getQuery()只返回已经包含表引用的底层构建器。

$browser_total_raw = DB::raw('count(*) as total');
$user_info = Usermeta::getQuery()
                     ->select('browser', $browser_total_raw)
                     ->groupBy('browser')
                     ->pluck('total','browser');

回答by Boris Tet?ev

  1. Open config/database.php
  2. Find strictkey inside mysqlconnection settings
  3. Set the value to false
  1. 打开 config/database.php
  2. strictmysql连接设置中查找密钥
  3. 将值设置为 false

回答by Jasim Juwel

Try with this

试试这个

->groupBy('state_id','locality')
  ->havingRaw('count > 1 ')
  ->having('items.name','LIKE',"%$keyword%")
  ->orHavingRaw('brand LIKE ?',array("%$keyword%"))

回答by Vulfoliac

Here is a more Laravel way to handle group by without the need to use raw statements.

这是一种更 Laravel 处理 group by 的方法,无需使用原始语句。

$sources = $sources->where('age','>', 31)->groupBy('age');

$output = null;
foreach($sources as $key => $source) {
    foreach($source as $item) {
        //get each item in the group
    }
    $output[$key] = $source->count();
}