leader; $model->set('leader_id_list', []); if (!empty($leader)) { $model->set('leader_id_list', array_column($leader->toArray() ?? [], 'id')); } } return $model; } /** * 获取部门子级所有id * * @param string|int $parentId * @param true $includeSelf * * @return array */ public function getChildIdsIncludingSelf(string|int $parentId, true $includeSelf = true): array { $allChildIds = new Collection(); if ($includeSelf) { $this->addIdToCollection($parentId, $allChildIds); } $this->collectChildIds($parentId, $allChildIds); return $allChildIds->unique()->values()->all(); } /** * 将子级id添加到collection * * @param $id * @param \Illuminate\Support\Collection $collection */ private function addIdToCollection($id, Collection &$collection): void { if (!$collection->contains($id)) { $collection->push($id); } } /** * 递归获取子级 * * @param $parentId * @param \Illuminate\Support\Collection $allChildIds */ private function collectChildIds($parentId, Collection &$allChildIds) { $childIds = Dept::where('pid', $parentId)->pluck('id'); foreach ($childIds as $childId) { $this->addIdToCollection($childId, $allChildIds); $this->collectChildIds($childId, $allChildIds); } } /** * 获取部门及所有子部门ID(包括自身) * * @param int $deptId * * @return array */ // public function getChildIdsIncludingSelf(int $deptId): array // { // $ids = collect(); // $this->collectChildIds($deptId, $ids); // return $ids->unique()->toArray(); // } // // /** // * 递归收集部门ID // * // * @param int $deptId // * @param \Illuminate\Support\Collection $ids // */ // private function collectChildIds(int $deptId, \Illuminate\Support\Collection &$ids): void // { // $ids->push($deptId); // $children = $this->model->where('pid', $deptId)->get(); // foreach ($children as $child) { // $this->collectChildIds($child->id, $ids); // } // } }