diff --git a/app/Admin/Controllers/CategoryController.php b/app/Admin/Controllers/CategoryController.php new file mode 100644 index 0000000..972af65 --- /dev/null +++ b/app/Admin/Controllers/CategoryController.php @@ -0,0 +1,82 @@ +header('产品分类') + ->body(function (Row $row) { + $tree = new Tree(new Category); + $tree->expand(); + $tree->query(function ($model) { + //agent_id为0是系统分类,其它是代理商分类 + return $model->where('agent_id', 0); + }); + $row->column(12, $tree); + }); + } + + /** + * Make a grid builder. + * + * @return Grid + */ + protected function grid() + { + return Grid::make(new Category(), function (Grid $grid) { + $grid->column('id')->sortable(); + $grid->column('pid'); + $grid->column('name'); + $grid->column('sort'); + + $grid->filter(function (Grid\Filter $filter) { + $filter->equal('id'); + + }); + }); + } + + /** + * Make a show builder. + * + * @param mixed $id + * + * @return Show + */ + protected function detail($id) + { + return Show::make($id, new Category(), function (Show $show) { + $show->field('id'); + $show->field('pid'); + $show->field('name'); + $show->field('sort'); + }); + } + + /** + * Make a form builder. + * + * @return Form + */ + protected function form() + { + return Form::make(new Category(), function (Form $form) { + $form->display('id'); + $form->select('pid')->options(Category::selectOptions()); + $form->text('name'); + $form->text('sort')->default(255); + }); + } +} diff --git a/app/Admin/Repositories/Category.php b/app/Admin/Repositories/Category.php new file mode 100644 index 0000000..10c4746 --- /dev/null +++ b/app/Admin/Repositories/Category.php @@ -0,0 +1,16 @@ +resource('agent/audit', 'AgentController'); $router->resource('guide/list', 'GuideController'); $router->resource('guide/audit', 'GuideController'); + $router->resource('category/list', 'CategoryController'); }); diff --git a/app/Models/Category.php b/app/Models/Category.php index efac1af..71d5f4d 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -2,15 +2,32 @@ namespace App\Models; +use Dcat\Admin\Traits\ModelTree; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; class Category extends BaseModel { use HasFactory; + use ModelTree; + + // 模型树需要的三个属性 + protected $parentColumn = 'pid'; + protected $orderColumn = 'sort'; + protected $titleColumn = 'name'; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->timestamps = false; } + + //覆盖父类,不按id desc排序 + protected static function booted() { } + + public static function selectOptions(\Closure $closure = null) + { + $options = (new static())->withQuery($closure)->buildSelectOptions(); + return collect($options)->all(); + } } diff --git a/resources/lang/zh_CN/category.php b/resources/lang/zh_CN/category.php new file mode 100644 index 0000000..affb729 --- /dev/null +++ b/resources/lang/zh_CN/category.php @@ -0,0 +1,15 @@ + [ + 'Category' => '产品分类', + 'category' => '产品分类', + ], + 'fields' => [ + 'agent_id' => '代理商ID', + 'pid' => '上级ID', + 'name' => '类目名称', + 'sort' => '排序', + ], + 'options' => [ + ], +];