我的MySQL返回值如下
{
"BU_id": 1,
"SE_id": 3815,
"SE_name": "Director",
},
{
"BU_id": 1,
"SE_id": 3816,
"SE_name": "Art Director",
},
{
"BU_id": 2,
"SE_id": 7032,
"SE_name": "Regular",
},
{
"BU_id": 2,
"SE_id": 7033,
"SE_name": "Member",
},
我希望我的返回数据按“BU_id”分组
{
"BU_id": 1,
{
{
"SE_id": 3815,
"SE_name": "Director",
},
{
"SE_id": 3816,
"SE_name": "Art Director",
},
},
"BU_id": 2,
{
{
"SE_id": 7032,
"SE_name": "Regular",
},
{
"SE_id": 7033,
"SE_name": "Member",
},
},
},
我尝试使用“groupBY('BU_id')”,但它完全删除了第二个数据,只显示了与BU_id对应的第一个数据
您可以在select语句中使用group_concat()函数将结果与','连接。
就像是:
Select bu_id, group_concat(column_1) from table group by bu_id;
这似乎是你需要的。
// If you get it from a model its already
$output = $service->keyBy('BU_id');
// If it is a normal array just collect it
$output = collect($service)->keyBy('BU_id');
使用Collection::groupBy方法按给定键对集合进行分组:
Model::all()->grouBy('BU_id');