123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\Model;
- use Illuminate\Database\Eloquent\Model;
- use Encore\Admin\Traits\AdminBuilder;
- use Encore\Admin\Traits\ModelTree;
- class Category extends Model
- {
- use ModelTree,AdminBuilder;
- public $table = "jm_category";
- protected $fillable = ['parent_id' , 'category_name' , 'id'];
- // 展示树状调用
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- $this->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;
- }
- }
- }
|