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 關注
- 183 瀏覽
添加回答
舉報