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

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