where(array("pid"=>0)) ->select(array("id","name")) ->get() ->toArray(); if (!empty($oneLevelData)){ $oneLevelData = array_column($oneLevelData,"name","id"); $oneLevelData[0] = "一级分类"; ksort($oneLevelData); } $grid = new Grid(new MentalityDrillCategory); $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){ // 在这里添加字段过滤器 $filter->like('name', '分类名称'); $filter->expand(); }); $grid->column('id', __('ID'))->sortable(); $grid->column('name', __('分类名称')); $grid->column('rank', __('排序(越大越前)')); $grid->column('is_index', __('是否首页'))->display(function ($isIndex){ return isset(MentalityDrillCategory::isIndexMap[$isIndex]) ? MentalityDrillCategory::isIndexMap[$isIndex] : "" ; }); $grid->column('pid', __('父级分类'))->display(function ($id) use ($grid,$oneLevelData){ if ($id > 0){ if (isset($oneLevelData[$id])){ return $oneLevelData[$id]; }else { $data = MentalityDrillCategory::query() ->newQuery() ->where(array("id"=>$id)) ->select(array("id","name")) ->first(); if (!empty($data)){ return $data->name; } } return ""; }else{ return $oneLevelData[0]; } }); $grid->column('is_subject', __('是否开启课程'))->display(function ($isSubject){ return isset(MentalityDrillCategory::isSubjectMap[$isSubject]) ? MentalityDrillCategory::isSubjectMap[$isSubject] : "" ; }); $grid->column('created_at', __('创建时间')); $grid->column('updated_at', __('更新时间')); $grid->actions(function (Grid\Displayers\Actions $actions){ // 去掉删除 $actions->disableDelete(); }); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(MentalityDrillCategory::findOrFail($id)); $show->field('id', __('ID')); $show->field('name', __('分类名称')); $show->field('created_at', __('Created at')); $show->field('updated_at', __('Updated at')); return $show; } /** * Make a form builder. * * @return Form */ protected function form() { $form = new Form(new MentalityDrillCategory); $oneLevelData = MentalityDrillCategory::query() ->where(array("pid"=>0)) ->select(array("id","name")) ->get() ->toArray(); if (!empty($oneLevelData)){ $oneLevelData = array_column($oneLevelData,"name","id"); } $form->display('id', __('ID')); $oneLevelData[0] = "一级分类"; ksort($oneLevelData); $form->select('pid', __('父级分类'))->options($oneLevelData)->default(0); $form->text('name', __('分类名称'))->rules('required'); $form->text('index_info', __('分类首页简介')); $form->image('index_pic', __('分类首页图片')); $form->image('pic', __('分类图片'))->uniqueName()->move('pics/'.date('Y').'/'.date('m'))->rules('required'); $form->text('rank', __('排序(越大越前)'))->default(0); $form->radio("is_index","是否首页") ->options(MentalityDrillCategory::isIndexMap) ->default(MentalityDrillCategory::isIndexNo) ; $form->radio("is_subject","是否开启课程") ->options(MentalityDrillCategory::isSubjectMap) ->default(MentalityDrillCategory::isSubjectNo) ; $form->display('created_at', __('创建时间')); $form->display('updated_at', __('更新时间')); $form->tools(function (Form\Tools $tools){ $tools->disableDelete(); }); return $form; } /** * 下级分类 * @param Request $request * @return array */ public function lowerLevel(Request $request) { $pid = $request->get("q",0); $oneLevelData = MentalityDrillCategory::query() ->where(array("pid"=>$pid)) ->select(array("id","name as text")) ->get() ->toArray(); return $oneLevelData; } }