has('_expand_')) { $CategoryMajor=CategoryMajor::find(request()->get('_expand_')); if (!$CategoryMajor || $CategoryMajor->children->isEmpty()) { $data['has_children'] =0; return response()->json($data); } $data['has_children'] =1; $data['html']=view( 'admin.grid.major_expand', [ 'parent_group'=>implode(" ", explode(",", request()->get('parent_group'))), 'key'=>request()->get('_expand_'), 'list'=>$CategoryMajor->children, 'level'=>request()->get('level')+1, ] )->render(); return response()->json($data); } return $content ->header('专业分类') ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。') ->body($this->grid()); } /** * Show interface. * * @param mixed $id * @param Content $content * @return Content */ public function show($id, Content $content) { return $content ->header('专业分类') ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。') ->body($this->detail($id)); } /** * Edit interface. * * @param mixed $id * @param Content $content * @return Content */ public function edit($id, Content $content) { return $content ->header('专业分类') ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。') ->body($this->editForm()->edit($id)); } /** * Create interface. * * @param Content $content * @return Content */ public function create(Content $content) { return $content ->header('专业分类') ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。') ->body($this->form()); } /** * Make a grid builder. * * @return Grid */ protected function grid() { $grid = new Grid(new CategoryMajor); $grid->model()->where('parent_id',0)->orderBy('order', 'desc'); $grid->id('ID'); $grid->name('名称')->ajaxExpand(); $grid->spell('拼音')->label(); $grid->order('排序'); $grid->created_at('添加时间'); $grid->updated_at('更新时间'); $grid->disableCreateButton(false); $grid->actions(function ($actions) { $actions->disableEdit(false); $actions->disableDelete(false); }); $grid->filter(function ($filter) { $filter->like('name', '名称'); }); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(CategoryMajor::findOrFail($id)); $show->id('ID'); $show->name('名称'); $show->spell('拼音')->label(); $show->order('排序')->label(); $show->created_at('添加时间'); $show->updated_at('更新时间'); return $show; } protected function editForm() { $form = new Form(new CategoryMajor); $array = $this->categoryMajorArr(CategoryMajor::all()->toArray(),0); $array = array_pluck($array,'name','id'); $option = ['0'=>'顶级'] +$array; $form->select('parent_id', '所属上级')->options($option)->setMustMark(); $form->text('name', '名称'); $form->text('spell', '拼音'); $form->number('order', '排序')->default(0)->min(0); return $form; } public function update($id) { return $this->editForm()->update($id); } /** * Make a form builder. * * @return Form */ protected function form() { $form = new Form(new CategoryMajor); $array = $this->categoryMajorArr(CategoryMajor::all()->toArray(),0); $array = array_pluck($array,'name','id'); $option = ['0'=>'顶级'] +$array; $form->select('parent_id', '所属上级')->options($option)->setMustMark(); $form->text('name', '名称'); $form->text('spell', '拼音'); $form->number('order', '排序')->default(0)->min(0); return $form; } public function destroy($id) { \DB::beginTransaction(); try { $res = CategoryMajor::all()->toArray(); $list = $this->categoryMajorArr($res,$id); $ids = array_pluck($list,'id'); CategoryMajor::where('id',$id)->delete(); CategoryMajor::whereIn('id',$ids)->delete(); $data = [ 'status' => true, 'message' => '删除成功!', ]; \DB::commit(); return response()->json($data); } catch (\Exception $e) { \DB::rollback(); $data = [ 'status' => false, 'message' => '删除失败!', ]; return response()->json($data); } } public function categoryMaj(Request $request) { $q = $request->get('q'); return CategoryMajor::where('parent_id', $q)->get(['id','name as text']); } //递归查询 protected function categoryMajorArr($data,$pid,$level=''){ static $arr=array(); foreach($data as $k => $v){ //如果该分类pid=0 if($v['parent_id']==$pid){ $v['name']= $level.$v['name']; $arr[]=$v; self::categoryMajorArr($data,$v['id'],$level.'        '); } } return $arr; } }