CategoryController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Model\Category;
  5. use Encore\Admin\Form;
  6. use Encore\Admin\Grid;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Controllers\ModelForm;
  10. class CategoryController extends Controller
  11. {
  12. use ModelForm;
  13. public function index()
  14. {
  15. // return Admin::content(function (Content $content) {
  16. // $content->header('类目');
  17. // $content->description('列表');
  18. // $content->body($this->grid());
  19. // });
  20. return Admin::content(function (Content $content) {
  21. $content->header('脑图列表');
  22. $content->body(Category::tree(function ($tree) {
  23. $tree->branch(function ($branch) {
  24. return "{$branch['id']} - {$branch['category_name']}";
  25. });
  26. }));
  27. });
  28. }
  29. public function create()
  30. {
  31. return Admin::content(function (Content $content) {
  32. $content->header('脑图');
  33. $content->description('新增');
  34. $content->body($this->form());
  35. });
  36. }
  37. public function edit($id)
  38. {
  39. return Admin::content(function (Content $content) use ($id) {
  40. $content->header('脑图');
  41. $content->description();
  42. $content->body($this->form($id)->edit($id));
  43. });
  44. }
  45. protected function form($id = '')
  46. {
  47. return Admin::form(Category::class, function (Form $form) {
  48. $form->display('id' , '编号');
  49. $form->text('category_name' , '分类名称');
  50. $form->select('parent_id' , '选择分类')->options(Category::getCategory());
  51. $form->select('type' , '选择类型')->options([1 => '脑图中文' , 2 => '脑图英文']);
  52. $form->saving(function (Form $form) {
  53. if(is_null($form->parent_id)) {
  54. $form->parent_id = 0;
  55. }
  56. });
  57. });
  58. }
  59. // Grid
  60. protected function grid()
  61. {
  62. return Admin::grid(Category::class, function (Grid $grid) {
  63. $grid->id('编号')->sortable();
  64. //$grid->model()->orderBy('id','desc');
  65. $grid->category_name('类目名称');
  66. $grid->parent_id('父级名称')->display(function ($parent_id) {
  67. return $parent_id ? Category::getCategoryName($parent_id): '---';
  68. });
  69. $grid->paginate(20);
  70. // 禁用导出数据按钮
  71. $grid->actions(function ($actions) {
  72. $actions->disableView();
  73. //$actions->disableEdit();
  74. $actions->disableDelete();
  75. });
  76. //$grid->disableCreateButton();
  77. $grid->disableExport();
  78. $grid->filter(function ($filter) {
  79. $filter->disableIdFilter();
  80. });
  81. });
  82. }
  83. }