海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.0 KiB

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Category;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Layout\Content;
  7. use Dcat\Admin\Layout\Row;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Tree;
  11. class CategoryController extends AdminController
  12. {
  13. public function index(Content $content)
  14. {
  15. return $content->header('产品分类')
  16. ->body(function (Row $row) {
  17. $tree = new Tree(new Category);
  18. $tree->expand();
  19. $tree->query(function ($model) {
  20. //agent_id为0是系统分类,其它是代理商分类
  21. return $model->where('agent_id', 0);
  22. });
  23. $row->column(12, $tree);
  24. });
  25. }
  26. /**
  27. * Make a grid builder.
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(new Category(), function (Grid $grid) {
  34. $grid->column('id')->sortable();
  35. $grid->column('pid');
  36. $grid->column('name');
  37. $grid->column('sort');
  38. $grid->column('template');
  39. });
  40. }
  41. /**
  42. * Make a show builder.
  43. *
  44. * @param mixed $id
  45. *
  46. * @return Show
  47. */
  48. protected function detail($id)
  49. {
  50. return Show::make($id, new Category(), function (Show $show) {
  51. $show->field('id');
  52. $show->field('pid');
  53. $show->field('name');
  54. $show->field('sort');
  55. $show->field('template');
  56. });
  57. }
  58. /**
  59. * Make a form builder.
  60. *
  61. * @return Form
  62. */
  63. protected function form()
  64. {
  65. return Form::make(new Category(), function (Form $form) {
  66. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  67. $form->display('id');
  68. $form->select('pid')->options($options)->required();
  69. $form->text('name')->required();
  70. $form->text('sort')->default(255)->help('越小越靠前');
  71. })->saving(function (Form $form) {
  72. //后台管理员的agent_id为0
  73. $form->agent_id = 0;
  74. $form->sort = $form->sort ?? 255;
  75. });
  76. }
  77. }