CategoryController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Admin\Controllers\Setting;
  3. use App\Category;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Grid;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Layout\Content;
  8. use App\Http\Controllers\Controller;
  9. use Encore\Admin\Controllers\ModelForm;
  10. class CategoryController extends Controller
  11. {
  12. use ModelForm;
  13. /**
  14. * Index interface.
  15. *
  16. * @return Content
  17. */
  18. public function index()
  19. {
  20. return Admin::content(function (Content $content) {
  21. $content->header('类型配置');
  22. $content->description('列表');
  23. $content->row(function ($row) use ($content) {
  24. $row->column(6, Category::tree());
  25. $row->column(6, $this->form()->render());
  26. });
  27. });
  28. }
  29. /**
  30. * Make a form builder.
  31. *
  32. * @return Form
  33. */
  34. protected function form()
  35. {
  36. return Admin::form(Category::class, function (Form $form) {
  37. $form->setAction(url('admin/setting/category'));
  38. $form->display('id', 'ID');
  39. $form->select('parent_id', '分类')->options(array_merge([0 => 'Root'], Category::getRoot()->toArray()));
  40. $form->text('title', '名称');
  41. $form->text('value', '值');
  42. $states = [
  43. 'on' => ['value' => 'T', 'text' => '是', 'color' => 'success'],
  44. 'off' => ['value' => 'F', 'text' => '否', 'color' => 'danger'],
  45. ];
  46. $form->switch('root', '是否是根目录')->states($states)->default('T');
  47. $form->display('created_at', 'Created At');
  48. $form->display('updated_at', 'Updated At');
  49. });
  50. }
  51. /**
  52. * Edit interface.
  53. *
  54. * @param $id
  55. * @return Content
  56. */
  57. public function edit($id)
  58. {
  59. return Admin::content(function (Content $content) use ($id) {
  60. $content->header('类型配置');
  61. $content->description('编辑');
  62. $content->body($this->form()->edit($id));
  63. });
  64. }
  65. /**
  66. * Create interface.
  67. *
  68. * @return Content
  69. */
  70. public function create()
  71. {
  72. return Admin::content(function (Content $content) {
  73. $content->header('类型配置');
  74. $content->description('创建');
  75. $content->body($this->form());
  76. });
  77. }
  78. /**
  79. * Make a grid builder.
  80. *
  81. * @return Grid
  82. */
  83. protected function grid()
  84. {
  85. return Admin::grid(Category::class, function (Grid $grid) {
  86. $grid->id('ID')->sortable();
  87. $grid->created_at();
  88. $grid->updated_at();
  89. });
  90. }
  91. }