setParentColumn('parent_id'); $this->setOrderColumn( 'id'); $this->setTitleColumn( 'category_name'); } // 获取分类名称 public static function getCategoryName($parent_id) { $category = self::find($parent_id); return $category->category_name; } public static function getCategory() { return self::getTree(self::all() , 0, 0); } public static function getTree($arr , $pid , $step) { global $tree; //$arr = self::all(); if($arr->isEmpty()) { return []; } else { foreach ($arr as $key => $val) { if ($val['parent_id'] == $pid) { $flg = str_repeat('└-', $step); $val['category_name'] = $flg . $val['category_name']; $tree[] = $val; self::getTree($arr , $val['id'], $step + 1); } } if(empty($tree)) return [0 => '1级']; $selectOption = []; foreach ($tree as $option) { $selectOption[$option->id] = $option->category_name; } $selectOption[0] = 'Root'; return $selectOption; } } }