带有 groupby、count 和 sum 的 Laravel 集合

标签 laravel collections

我正在努力让一个集合的 groupby 工作 - 我还没有得到这个概念。
我正在为玩家从表格中提取一组结果, Eloquent 集合将包含如下数据:

['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'won', 'points'=>2],
我希望能够groupBy('opposition_id')然后给我一个总的结果,总赢,总输和总分,最终得到一个这样的集合:
['opposition_id'=>10, 'results'=>3, 'won'=>2, 'lost'=>1, 'points'=>4],
['opposition_id'=>11, 'results'=>2, 'won'=>0, 'lost'=>2, 'points'=>0],
['opposition_id'=>10, 'results'=>2, 'won'=>1, 'lost'=>1, 'points'=>2]
我试图避免返回数据库来执行此操作,因为我已经获得了先前事件的结果。
我如何使用 Laravel 收集方法来做到这一点,到目前为止我所拥有的是:
$stats = $results->groupBy('opposition_id');
我看过 map()但还不了解解决方案的方法
任何人都可以指出我正确的方向。
如果需要,很高兴回到数据库,但假设我可以用我已经拥有的集合来做到这一点,而不是创建另一个查询。我在这里找到的解决方案似乎都在查询中提供了解决方案
谢谢

最佳答案

看看这里,在注释中解释的工作代码。

// make a collection
$c = collect(
    [
        ['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
        ['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
        ['player_id' => 1, 'opposition_id' => 12, 'result' => 'lost', 'points' => 0],
        ['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
        ['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
        ['player_id' => 1, 'opposition_id' => 10, 'result' => 'lost', 'points' => 0],
        ['player_id' => 1, 'opposition_id' => 12, 'result' => 'won', 'points' => 2]
    ]
);
// this only splits the rows into groups without any thing else.
// $groups will be a collection, it's keys are 'opposition_id' and it's values collections of rows with the same opposition_id.
$groups = $c->groupBy('opposition_id'); 

// we will use map to cumulate each group of rows into single row.
// $group is a collection of rows that has the same opposition_id.
$groupwithcount = $groups->map(function ($group) {
    return [
        'opposition_id' => $group->first()['opposition_id'], // opposition_id is constant inside the same group, so just take the first or whatever.
        'points' => $group->sum('points'),
        'won' => $group->where('result', 'won')->count(),
        'lost' => $group->where('result', 'lost')->count(),
    ];
});
// if you don't like to take the first opposition_id you can use mapWithKeys:
$groupwithcount = $groups->mapWithKeys(function ($group, $key) {
    return [
        $key =>
            [
                'opposition_id' => $key, // $key is what we grouped by, it'll be constant by each  group of rows
                'points' => $group->sum('points'),
                'won' => $group->where('result', 'won')->count(),
                'lost' => $group->where('result', 'lost')->count(),
            ]
    ];
});

// here $groupwithcount will give you objects/arrays keyed by opposition_id:
[
  10 =>   ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
  11 =>   ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
  12 =>   ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]

// if you use $groupwithcount->values() it'll reset the keys to 0 based sequence as usual:
[
  0 =>   ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
  1 =>   ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
  2 =>   ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]

关于带有 groupby、count 和 sum 的 Laravel 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550381/

相关文章:

php - 查询生成器 : passing argument to anonymous function

php - Laravel 项目中解析错误 : syntax error, 意外 'const' (T_CONST),期望变量 (T_VARIABLE)

laravel - 为什么 Laravel Vapor 在我的构建步骤中没有注入(inject) ASSET_URL?

java - 返回 Hashset 中具有最高值的类对象

Laravel:在 View 中使用 "where"?

java - Java 中的不可变集合

java - 集合到int数组的转换

php - 在 Eloquent 中模拟右连接

java - java中Stack和Queue的内存是如何分配的

php - phpunit 中未定义的 action()