header($this->title) ->description('分类列表') ->body(function (Row $row) { $tree = new Tree(new ItemCategoryRepository()); $tree->branch(function ($branch) { $icon = $branch['icon'] ? "" : ''; return "{$icon} {$branch['name']} ({$branch['code']})"; }); $row->column(12, $tree); }); } /** * 列表页 * * @return Grid */ protected function grid() { return Grid::make(new ItemCategoryRepository(), function (Grid $grid) { $helper = new GridHelper($grid, $this); $helper->columnId(); $grid->column('name', '名称'); $grid->column('code', '编码'); $grid->column('icon', '图标')->image('', 50, 50); $grid->column('sort', '排序')->sortable(); $grid->column('parent.name', '父分类'); $grid->column('created_at', '创建时间'); $grid->column('updated_at', '更新时间'); // 筛选 $grid->filter(function ($filter) { $helper = new FilterHelper($filter, $this); $helper->equal('id','ID'); $filter->like('name', '名称'); $filter->like('code', '编码'); $filter->equal('parent_id', '父分类')->select( (new ItemCategoryRepository())->pluck('name', 'id') );}); }); return $grid; } /** * 详情页 * * @param mixed $id * @return Show */ protected function detail($id) { return Show::make((new ItemCategoryRepository())->findOrFail($id), function (Show $show) { $helper = new ShowHelper($show, $this); $helper->field('id', 'ID'); $helper->field('name', '名称'); $helper->field('code', '编码'); $show->field('icon', '图标')->image(); $helper->field('sort', '排序'); $show->field('parent.name', '父分类'); $helper->field('created_at', '创建时间'); $helper->field('updated_at', '更新时间'); // 显示子分类 $show->children('子分类', function ($children) { $children->resource('/admin/game-items-categories'); $children->id('ID'); $children->name('名称'); $children->code('编码'); $children->icon('图标')->image('', 50, 50); $children->sort('排序'); }); // 显示分类下的物品 $show->items('物品', function ($items) { $items->resource('/admin/game-items'); $items->id('ID'); $items->name('名称'); $items->icon('图标')->image('', 50, 50); $items->type('类型'); }); }); } /** * 表单 * * @return Form */ protected function form() { return Form::make(new ItemCategoryRepository(), function (Form $form) { $helper = new FormHelper($form, $this); $helper->text('name')->required(); $helper->text('code')->required()->help('用于系统识别的唯一编码'); $form->image('icon', '图标') ->uniqueName() ->autoUpload(); $helper->number('sort') ->default(0) ->help('数字越小越靠前'); // 父分类选择,排除自己及其子分类 $form->select('parent_id', '父分类') ->options(function () use ($form) { $categories = (new ItemCategoryRepository())->all(); $options = [0 => '无']; // 如果是编辑状态,需要排除自己及其子分类 if ($form->isEditing()) { $id = $form->getKey(); $childrenIds = $this->getChildrenIds($id); $childrenIds[] = $id; foreach ($categories as $category) { if (!in_array($category->id, $childrenIds)) { $options[$category->id] = $category->name; } } } else { foreach ($categories as $category) { $options[$category->id] = $category->name; } } return $options; }) ->default(0); }); } /** * 获取所有子分类ID * * @param int $categoryId * @return array */ protected function getChildrenIds(int $categoryId): array { $ids = []; $children = (new ItemCategoryRepository())->where('parent_id', $categoryId)->get(); foreach ($children as $child) { $ids[] = $child->id; $ids = array_merge($ids, $this->getChildrenIds($child->id)); } return $ids; } }