Laravel Collection 与 groupby、count 和 sum

2024-03-29

我正在努力让一个集合上的 groupby 工作 - 我还没有得到这个概念。

我正在从玩家的表中提取结果集合,雄辩的集合将包含如下数据:

['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]
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel Collection 与 groupby、count 和 sum 的相关文章

随机推荐