column('id')->sortable(); $grid->column('name'); $grid->column('icon')->image(50, 50); $grid->column('parent.name', '上级分类')->label(); $grid->column('is_show') ->if(function () { return Admin::user()->can('dcat.admin.product_categories.edit'); }) ->then(function (Column $column) { $column->switch(); }) ->else(function (Column $column) { $column->bool(); }); $grid->column('is_recommend') ->if(function () { return Admin::user()->can('dcat.admin.product_categories.edit'); }) ->then(function (Column $column) { $column->switch(); }) ->else(function (Column $column) { $column->bool(); }); $grid->column('sort'); $grid->column('created_at')->sortable(); //排序 $grid->model()->orderBy('created_at', 'desc'); /** 操作 **/ //新增 if (Admin::user()->can('dcat.admin.product_categories.create')) { $grid->disableCreateButton(false); $grid->enableDialogCreate(); } //修改 $grid->showQuickEditButton(Admin::user()->can('dcat.admin.product_categories.edit')); //删除以及自定义操作 $grid->actions(function (Grid\Displayers\Actions $actions) { $actions->disableDelete(Admin::user()->cannot('dcat.admin.product_categories.destroy')); }); /** 查询 **/ $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->equal('name')->width(3); $filter->equal('parent_id', '上级分类')->select(ProductCategoryModel::selectOptions())->width(3); }); }); } /** * Make a show builder. * * @param mixed $id * * @return Show */ protected function detail($id) { return Show::make($id, new ProductCategory(), function (Show $show) { $show->field('id'); $show->field('name'); $show->field('icon'); $show->field('is_show'); $show->field('sort'); $show->field('parent_id'); $show->field('created_at'); $show->field('updated_at'); }); } /** * Make a form builder. * * @return Form */ protected function form() { return Form::make(new ProductCategory(), function (Form $form) { $form->display('id'); $form->select('parent_id')->options(ProductCategoryModel::selectOptions()); $form->text('name')->required(); $form->image('icon') ->uniqueName() ->move('product-categories/'.Carbon::now()->toDateString()) ->saveFullUrl() ->removable(false) ->autoUpload()->retainable(); $form->switch('is_show'); $form->switch('is_recommend'); $form->number('sort')->min(0)->default(0); $form->saving(function (Form $form) { //只能添加到三级分类 if ($form->parent_id) { $result = ProductCategoryModel::withDepth()->find($form->parent_id); if ($result->depth >= 2) { return $form->response()->error('不能创建四级以及以下的分类'); } } }); $form->display('created_at'); $form->display('updated_at'); }); } public function destroy($id) { //如果有子分类或者分类下有商品则不允许删除 if (ProductCategoryModel::descendantsOf($id, ['id'])->count() > 0 || ProductSpu::where('category_id', $id)->count() > 0) { throw new BizException(__('product-category.options.deny_message')); } return parent::destroy($id); } public function categories(Request $request) { $query = ProductCategoryModel::select('id', 'name as text'); if ($request->filled('pid')) { $query->where('parent_id', $request->input('pid')); } if ($request->filled('level')) { $query->withDepth()->having('depth', '=', $request->input('level')); } $data = $query->get(); return response()->json($data); } }