//可以根据子级id 返回所有的父级id static public function getParentsInstitutions($id, $cate = null) { //$arr=array(); if (! $cate) { $cate = InstitutionModel::where(['type' => self::TYPE_MANAGEMENT_CENTER]) ->whereOr(['id' => $id]) ->column('parent_id', 'id'); } $parentId = intval($cate[$id]); if (! empty($parentId)) { return array_merge([$parentId] ,self::getParentsInstitutions($parentId, $cate)); // 继续递归查父级的父级 } else { return []; } }
public function get_category2($category_id){ $InstitutionModel=new InstitutionModel(); $category_ids = $category_id.","; $child_category = $InstitutionModel->where("type <> 2 and parent_id=$category_id")->column('id'); foreach( $child_category as $val ) { $category_ids .= $this->get_category2($val); } //$category_ids=rtrim($category_ids,','); return $category_ids; } public function get_category($category_id){ return rtrim($this->get_category2($category_id), ','); }