getGroupTask($project, $group); $groups = $groupTasks->keys()->filter(); $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group); $items = []; foreach(["", ...$groups] as $groupKey) { if (! isset($groupTasks[$groupKey])) { continue; } $items[] = [ 'group' => $group, 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey], 'tasks' => ProjectGanttResource::collection($groupTasks[$groupKey]), ]; } return $items; } protected function getGroupNamesKeyBy(Collection $groups, string $group): array { $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]); $groupNames = match ($group) { "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(), "task_type" => $groupsFormat, "assign" => User::query()->whereIn("id", $groups)->get(['id', 'name']), }; return $groupNames->keyBy("id")->toArray(); } protected function getGroupTask( Project $project, string $group, ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array { return Task::query() ->with(['assignTo', 'finishedBy']) ->where("project_id", $project->id) ->get() ->groupBy($group); } }