海南旅游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.

86 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->filter(function (Grid\Filter $filter) {
  39. $filter->equal('id');
  40. });
  41. });
  42. }
  43. /**
  44. * Make a show builder.
  45. *
  46. * @param mixed $id
  47. *
  48. * @return Show
  49. */
  50. protected function detail($id)
  51. {
  52. return Show::make($id, new Category(), function (Show $show) {
  53. $show->field('id');
  54. $show->field('pid');
  55. $show->field('name');
  56. $show->field('sort');
  57. });
  58. }
  59. /**
  60. * Make a form builder.
  61. *
  62. * @return Form
  63. */
  64. protected function form()
  65. {
  66. return Form::make(new Category(), function (Form $form) {
  67. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  68. $form->display('id');
  69. $form->select('pid')->options($options)->required();
  70. $form->text('name')->required();
  71. $form->text('sort')->default(255);
  72. })->saving(function (Form $form) {
  73. //后台管理员的agent_id为0
  74. $form->agent_id = 0;
  75. });
  76. }
  77. }