PHP通过数据库的方式递归查询当前分类ID的所有子分类ID

/**
     * 通过数据库的方式递归查询当前分类ID的所有子分类ID
     * @param $id
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function findAllChild($id): array
    {
        static $arr = [];
        $members = self::where('pid', $id)->select()->toArray();
        foreach ($members as $member){
            $parent = self::find($member['id'])->toArray();
            $arr[] = $parent;
            self::findAllChild($parent['id']);
        }
        return $arr;
    }

 

posted @ 2022-12-23 23:26  _迷途  阅读(112)  评论(0编辑  收藏  举报