我一直在努力研究如何从Eloquent中的集合中获取不同值的数量。
我一直在尝试docs上的几个方法,例如unique(),values()等。即使确实存在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
)
但是在Eloquent系列中......
我怎样才能做到这一点?
答案 0 :(得分:11)
如果你真的想用收集方法做到这一点,那么就在这里:
$degrees->groupBy('Degree')->map(function ($people) {
return $people->count();
});
// Collection {
// 'Master' => 13,
// 'Bachelor' => 9,
// 'PhD' => 3
// }
答案 1 :(得分:0)
还可以执行以下操作:
__call
答案 2 :(得分:0)
有一个名为CountBy的集合帮助器,可以完全满足您的需求。
$degrees->countBy('Degree');
它将如期返回
Array
(
[Master] => 13
[Bachelor] => 9
[PhD] => 3
)
简单:D