1 回答
TA贡献1824条经验 获得超8个赞
问题是 :
$makeToCollection = collect($arr)->groupBy('year');
该groupBy方法按给定键对集合的项目进行分组
只需使用:
$makeToCollection = collect($arr);
顺便说一句,collection 允许你链接它的方法来执行流畅的映射和减少:
$historyPoints = $this->historyPoint
->select(
DB::raw("REGEXP_REPLACE(to_char(created_at, 'Month'), '\s+$', '') as month"),
DB::raw("date_part('Year', created_at) as year")
)
->groupBy('year', 'month')
->orderBy('year', 'desc')
->get();
/***** Here we go *****/
$makeToCollection = $historyPoints->map(function ($item) {
$history_data = [];
foreach (HistoryPointAdd::all() as $data) {
$month = date('F', strtotime($data->created_at));
$year = date('Y', strtotime($data->created_at));
if ($month == $historyPoint->month && $year == $historyPoint->year) {
$history_data[] = $data;
}
}
$item['history_data'] = $history_data;
return $item;
});
return $this->sendSuccess($this->successMessage, $makeToCollection);
- 1 回答
- 0 关注
- 156 浏览
添加回答
举报