getGroupTask($project, $group); $groups = $groupTasks->keys()->filter(); $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group); $items = []; foreach(["", ...$groups] as $groupKey) { if (! isset($groupTasks[$groupKey])) { continue; } $group_label = $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey]; foreach ($groupTasks[$groupKey] as $tasks){ if ($tasks->parent_id===0){ //若该任务为顶层任务,则取其任务名以及id,否则找该任务的父级id和父级name $tasks->group_label_id=$group_label['id']; $tasks->group_label_name=$group_label['name']; }else{ $tasks->group_label_id=$tasks->parent_id; $tasks->group_label_name=$tasks->parent->name; } } $items[] = [ // 'group' => $group, // 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey], ProjectGanttResource::collection($groupTasks[$groupKey]), ]; }; $result=[]; foreach ($items as $item){ foreach ($item as $it){ foreach ($it as $i){ $result[]= $i; } } } return $result; } 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); } }