当前位置: 首页 > 知识库问答 >
问题:

如何计算Laravel中集合的不同值?

杜轩昂
2023-03-14

我一直在努力如何从雄辩的收藏中获得不同价值的数量。

我一直在尝试几种方法,如独特(),值()等。,在文档上找到的。即使确实有一个count()方法,也没有方法来获取不同值的计数。

例如,通过应用以下查询

$technicalshighestdegrees = Capsule::table('academicinfo AS fa')
        ->selectRaw('DISTINCT fa.academic_id AS Id,c.name AS Degree')
        ->leftJoin('academics AS a','fa.academic_id','=','a.id')
        ->leftJoin('cat_degree AS c','fa.level','=','c.id')
        ->whereIn('a.type',['Technical'])
        ->where('a.status','!=','Retired')
        ->where('c.degree',true)
        ->orderBy('a.id')
        ->orderBy('c.hierarchy','desc')/*http://stackoverflow.com/a/17006377/1883256*/
        ->get();

我得到这个收藏:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 3
                    [Grado] => Master
                )

            [1] => stdClass Object
                (
                    [Id] => 3
                    [Grado] => Bachelor
                )

            [2] => stdClass Object
                (
                    [Id] => 4
                    [Grado] => Master
                )

            [3] => stdClass Object
                (
                    [Id] => 4
                    [Grado] => Bachelor
                )

            [4] => stdClass Object
                (
                    [Id] => 6
                    [Grado] => Master
                )

            [5] => stdClass Object
                (
                    [Id] => 6
                    [Grado] => Bachelor
                )

            [6] => stdClass Object
                (
                    [Id] => 18
                    [Grado] => Bachelor
                )

            [7] => stdClass Object
                (
                    [Id] => 27
                    [Grado] => Bachelor
                )

            [8] => stdClass Object
                (
                    [Id] => 34
                    [Grado] => Master
                )

            [9] => stdClass Object
                (
                    [Id] => 34
                    [Grado] => Bachelor
                )

            [10] => stdClass Object
                (
                    [Id] => 36
                    [Grado] => PhD
                )

            [11] => stdClass Object
                (
                    [Id] => 36
                    [Grado] => Master
                )

            [12] => stdClass Object
                (
                    [Id] => 36
                    [Grado] => Bachelor
                )

            [13] => stdClass Object
                (
                    [Id] => 37
                    [Grado] => Bachelor
                )

            [14] => stdClass Object
                (
                    [Id] => 42
                    [Grado] => PhD
                )

            [15] => stdClass Object
                (
                    [Id] => 42
                    [Grado] => Master
                )

            [16] => stdClass Object
                (
                    [Id] => 42
                    [Grado] => Bachelor
                )

            [17] => stdClass Object
                (
                    [Id] => 50
                    [Grado] => Bachelor
                )

            [18] => stdClass Object
                (
                    [Id] => 52
                    [Grado] => Bachelor
                )

            [19] => stdClass Object
                (
                    [Id] => 53
                    [Grado] => Master
                )

            [20] => stdClass Object
                (
                    [Id] => 53
                    [Grado] => Bachelor
                )

            [21] => stdClass Object
                (
                    [Id] => 54
                    [Grado] => Master
                )

            [22] => stdClass Object
                (
                    [Id] => 54
                    [Grado] => Bachelor
                )

            [23] => stdClass Object
                (
                    [Id] => 55
                    [Grado] => Master
                )

            [24] => stdClass Object
                (
                    [Id] => 55
                    [Grado] => Bachelor
                )

            [25] => stdClass Object
                (
                    [Id] => 57
                    [Grado] => Bachelor
                )

            [26] => stdClass Object
                (
                    [Id] => 68
                    [Grado] => Master
                )

            [27] => stdClass Object
                (
                    [Id] => 68
                    [Grado] => Bachelor
                )

            [28] => stdClass Object
                (
                    [Id] => 72
                    [Grado] => Master
                )

            [29] => stdClass Object
                (
                    [Id] => 72
                    [Grado] => Bachelor
                )

            [30] => stdClass Object
                (
                    [Id] => 77
                    [Grado] => Bachelor
                )

            [31] => stdClass Object
                (
                    [Id] => 82
                    [Grado] => Bachelor
                )

            [32] => stdClass Object
                (
                    [Id] => 85
                    [Grado] => PhD
                )

            [33] => stdClass Object
                (
                    [Id] => 85
                    [Grado] => Master
                )

            [34] => stdClass Object
                (
                    [Id] => 85
                    [Grado] => Bachelor
                )

            [35] => stdClass Object
                (
                    [Id] => 92
                    [Grado] => Master
                )

            [36] => stdClass Object
                (
                    [Id] => 92
                    [Grado] => Bachelor
                )

            [37] => stdClass Object
                (
                    [Id] => 100
                    [Grado] => Master
                )

            [38] => stdClass Object
                (
                    [Id] => 100
                    [Grado] => Bachelor
                )

            [39] => stdClass Object
                (
                    [Id] => 111
                    [Grado] => Bachelor
                )

            [40] => stdClass Object
                (
                    [Id] => 117
                    [Grado] => Master
                )

            [41] => stdClass Object
                (
                    [Id] => 117
                    [Grado] => Bachelor
                )

            [42] => stdClass Object
                (
                    [Id] => 123
                    [Grado] => Master
                )

            [43] => stdClass Object
                (
                    [Id] => 123
                    [Grado] => Bachelor
                )

        )

)

因为我只想得到最高的度数(一个用户Id可能有几个历史度数),所以我应用unique()方法,根据文档,它只留下第一个原始值,在我的例子中,我已经按层次结构desc对它们进行了排序:

$technicalshighestdegrees = $technicalshighestdegrees->unique('Id');

现在我得到了以下收藏(减少到25件,这正是总数):

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 3
                    [Degree] => Master
                )

            [2] => stdClass Object
                (
                    [Id] => 4
                    [Degree] => Master
                )

            [4] => stdClass Object
                (
                    [Id] => 6
                    [Degree] => Master
                )

            [6] => stdClass Object
                (
                    [Id] => 18
                    [Degree] => Bachelor
                )

            [7] => stdClass Object
                (
                    [Id] => 27
                    [Degree] => Bachelor
                )

            [8] => stdClass Object
                (
                    [Id] => 34
                    [Degree] => Master
                )

            [10] => stdClass Object
                (
                    [Id] => 36
                    [Degree] => PhD
                )

            [13] => stdClass Object
                (
                    [Id] => 37
                    [Degree] => Bachelor
                )

            [14] => stdClass Object
                (
                    [Id] => 42
                    [Degree] => PhD
                )

            [17] => stdClass Object
                (
                    [Id] => 50
                    [Degree] => Bachelor
                )

            [18] => stdClass Object
                (
                    [Id] => 52
                    [Degree] => Bachelor
                )

            [19] => stdClass Object
                (
                    [Id] => 53
                    [Degree] => Master
                )

            [21] => stdClass Object
                (
                    [Id] => 54
                    [Degree] => Master
                )

            [23] => stdClass Object
                (
                    [Id] => 55
                    [Degree] => Master
                )

            [25] => stdClass Object
                (
                    [Id] => 57
                    [Degree] => Bachelor
                )

            [26] => stdClass Object
                (
                    [Id] => 68
                    [Degree] => Master
                )

            [28] => stdClass Object
                (
                    [Id] => 72
                    [Degree] => Master
                )

            [30] => stdClass Object
                (
                    [Id] => 77
                    [Degree] => Bachelor
                )

            [31] => stdClass Object
                (
                    [Id] => 82
                    [Degree] => Bachelor
                )

            [32] => stdClass Object
                (
                    [Id] => 85
                    [Degree] => PhD
                )

            [35] => stdClass Object
                (
                    [Id] => 92
                    [Degree] => Master
                )

            [37] => stdClass Object
                (
                    [Id] => 100
                    [Degree] => Master
                )

            [39] => stdClass Object
                (
                    [Id] => 111
                    [Degree] => Bachelor
                )

            [40] => stdClass Object
                (
                    [Id] => 117
                    [Degree] => Master
                )

            [42] => stdClass Object
                (
                    [Id] => 123
                    [Degree] => Master
                )

        )

)

我想得到的是每个不同度数值的数量,如下所示:

Array
(
    [Master] => 13
    [Bachelor] => 9
    [PhD] => 3
)

但在一个雄辩的收藏中。。。

我怎样才能做到这一点?

共有3个答案

邵献
2023-03-14

还可以执行以下操作:

$master = $degrees->sum('Master');
return $master;

$bachelor = $degrees->sum('Bachelor');
return $bachelor;

$phD = $degrees->sum('PhD');
return $phD;
锺英彦
2023-03-14

有一个名为CountBy的集合帮助程序,它完全满足您的需要。

收藏计数

$degrees->countBy('Degree');

它会像预期的那样重新回归

 Array
(
    [Master] => 13
    [Bachelor] => 9
    [PhD] => 3
)

简单:D

廖弘量
2023-03-14

2021/L6的更新

谢谢@JeremyWadhams

$degrees->groupBy('Degree')->map(fn ($people) => $people->count());
// or using HigherOrder proxy on the collection
$degrees->groupBy('Degree')->map->count();

使用收集方法,这将是它:

$degrees->groupBy('Degree')->map(function ($people) {
    return $people->count();
});

// Collection {
//   'Master' => 13,
//   'Bachelor' => 9,
//   'PhD' => 3
// }
 类似资料:
  • 如何得到这个数据的乘法和。 这是我的kvit表 这是手术台 这里是关系 这是控制器 这是我的看法 我需要将每种产品的数量乘以价格,得到总金额。这是收据的总金额(1015510)=200怎么可能?

  • 问题内容: 说,有两个哈希集,如何计算它们的交集? 问题答案: 使用以下方法: 如果要保留集合,请创建一个新集合以保存交集: 该的的说,这正是你想要的: 仅保留此集合中包含在指定集合中的元素(可选操作)。换句话说,从该集合中删除所有未包含在指定集合中的元素。如果指定的集合也是一个集合,则此操作将有效地修改此集合,以使其值为两个集合的交集。

  • 我目前有API返回的以下json响应。我能用拉威尔的口才把它还给你。有几个用户,每个用户都有几个收据。收据具有类型和状态。我想尝试获取与类型和状态相关的每个收据的总金额。我能够使用 我尝试使用多个laravel集合方法https://laravel.com/docs/5.8/collections#available-methods但我仍然无法获得所需的响应。 我希望得到下面的答案

  • 如果一个集合中有大量的文档,我是否可以在不加载整个集合的情况下获得文档的计数。

  • 我想计算两个集合中不同元素的数量。这就是集合1和集合2的并集。 如何将以下伪SQL格式化为有效的SQL 编辑set1和set2是表1的两列 下面是一个简短的例子: 在一个班里安排1名学生: 在一个班里安排两名学生: 我想要两个班的学生人数不同: 所以,一个工会就行了,我可以数一数。