Laravel 按日期分组
Laravel Grouping By date
我阅读了一些已经发布的问题 here.But 没有找到满足我要求的任何解决方案。
请记住此响应显示有关用户访问商店的签到信息。
分组收集
$this->collection->groupBy(function ($date) {
return Carbon::parse($date->createdAt)->toDateString();
}
将返回按日期分组的数据..
回应
{
"data":{
"2021-08-31":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
],
"2021-08-29":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
],
"2021-08-20":[
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
},
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
},
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
}
]
}
}
但我希望回复看起来像炸了..
然后分组日期将显示为..
{
"data":[
{
"date":"2021-08-31",
"data":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
]
},
{
"date":"2021-08-29",
"data":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
]
}
]
}
您已经收到了您需要的数据。
只是为了将数据固定在所需的格式循环中你收到的集合和
将其设置为您希望的格式。
您无法在检索数据时在 Mysql 级别修改该响应。你可以做的是在你的集合解析它并相应地修改它之后。这样的事情会起作用:
$counter = 0;
foreach($myCollection->data as $key=>$value){
$formattedResponse['data'][$counter]['date'] = $key;
$formattedResponse['data'][$counter]['data'] = $value;
$counter ++;
}
基于您的 json 输入示例的输出是:
{
"data": [{
"date": "2021-08-31",
"data": [{
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}, {
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}]
}, {
"date": "2021-08-29",
"data": [{
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}]
}, {
"date": "2021-08-20",
"data": [{
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}, {
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}, {
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}]
}]
}
我阅读了一些已经发布的问题 here.But 没有找到满足我要求的任何解决方案。 请记住此响应显示有关用户访问商店的签到信息。
分组收集
$this->collection->groupBy(function ($date) {
return Carbon::parse($date->createdAt)->toDateString();
}
将返回按日期分组的数据..
回应
{
"data":{
"2021-08-31":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
],
"2021-08-29":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
],
"2021-08-20":[
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
},
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
},
{
"avatar":"avatars/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name":"XYZ Store"
}
]
}
}
但我希望回复看起来像炸了.. 然后分组日期将显示为..
{
"data":[
{
"date":"2021-08-31",
"data":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
]
},
{
"date":"2021-08-29",
"data":[
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
},
{
"avatar":"avatars/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name":"General Shop 15"
}
]
}
]
}
您已经收到了您需要的数据。 只是为了将数据固定在所需的格式循环中你收到的集合和 将其设置为您希望的格式。
您无法在检索数据时在 Mysql 级别修改该响应。你可以做的是在你的集合解析它并相应地修改它之后。这样的事情会起作用:
$counter = 0;
foreach($myCollection->data as $key=>$value){
$formattedResponse['data'][$counter]['date'] = $key;
$formattedResponse['data'][$counter]['data'] = $value;
$counter ++;
}
基于您的 json 输入示例的输出是:
{
"data": [{
"date": "2021-08-31",
"data": [{
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}, {
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}]
}, {
"date": "2021-08-29",
"data": [{
"avatar": "avatars\/IIa1gvCvww5K4geXJHUYTTZm2IKFoK113octfPB1.jpg",
"name": "General Shop 15"
}]
}, {
"date": "2021-08-20",
"data": [{
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}, {
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}, {
"avatar": "avatars\/GSDrGe8RLGXykvvsEVlWtnqRGbAOoJUuSh7WZhFc.jpg",
"name": "XYZ Store"
}]
}]
}