user(); /** @var \Illuminate\Database\Eloquent\Collection */ $taskPerformances = TaskPerformance::with(['task']) ->where('store_id', $user->store_id) ->when($request->input('filter'), function ($query, $filter) { $now = now(); switch ($filter) { case 'future': $query->whereHas('task', fn ($query) => $query->where('end_at', '>', $now)); break; case 'history': $query->whereHas('task', fn ($query) => $query->where('end_at', '<=', $now)); break; } }) ->get(); return TaskPerformanceResource::collection($taskPerformances); // return $taskPerformances->groupBy(fn (TaskPerformance $taskable) => Carbon::createFromFormat('Y-m', $taskable->month)->year) // ->map(function (Collection $collection) { // return $collection->mapWithKeys(function (TaskPerformance $taskable) { // return [ // Carbon::createFromFormat('Y-m', $taskable->month)->month => [ // 'id' => $taskable->id, // 'month' => $taskable->month, // 'actual_performance' => trim_zeros($taskable->actual_performance), // 'expected_performance' => trim_zeros($taskable->expected_performance), // 'status' => $taskable->task_status, // ], // ]; // })->sortKeysDesc(); // }); } }