CategoryController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Category;
  5. use App\Models\CategoryGroups;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. class CategoryController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('其它分类管理')
  25. ->description('')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('其它分类管理')
  39. ->description('详细')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('其它分类管理')
  53. ->description('编辑')
  54. ->body($this->editForm($id)->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('其它分类管理')
  66. ->description('创建')
  67. ->body($this->createForm());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new Category);
  77. $grid->id('ID');
  78. $grid->category_name('名称');
  79. $grid->alias('别名');
  80. $grid->demand('需求');
  81. $grid->order('排序');
  82. $grid->created_at('添加时间');
  83. $grid->updated_at('更新时间');
  84. $grid->actions(function ($actions) use ($grid) {
  85. if (Admin::user()->can('system_type_other_list_edit')) {
  86. $actions->disableEdit(false);
  87. }
  88. if (Admin::user()->can('system_type_other_list_delete')) {
  89. $actions->disableDelete(false);
  90. }
  91. });
  92. if (Admin::user()->can('system_type_other_list_delete')) {
  93. $grid->tools(function ($tools) {
  94. $tools->batch(function ($batch) {
  95. $batch->disableDelete(false);
  96. });
  97. });
  98. $grid->disableRowSelector(false);
  99. }
  100. $grid->filter(function ($filter) {
  101. $data = CategoryGroups::all(['name','alias']);
  102. foreach ($data as $key => $val) {
  103. $res[$key]['name'] = $val['name'];
  104. $res[$key]['alias'] = $val['alias'];
  105. }
  106. $filter->equal('alias', '名称')->select(array_column($res, 'name', 'alias'));
  107. });
  108. if (Admin::user()->can('system_type_other_list_create')) {
  109. $grid->disableCreateButton(false);
  110. }
  111. return $grid;
  112. }
  113. /**
  114. * Make a show builder.
  115. *
  116. * @param mixed $id
  117. * @return Show
  118. */
  119. protected function detail($id)
  120. {
  121. $show = new Show(Category::findOrFail($id));
  122. $show->id('ID');
  123. $show->category_name('名称');
  124. $show->demand('需求');
  125. $show->alias('别名');
  126. $show->order('排序');
  127. $show->created_at('添加时间');
  128. $show->updated_at('更新时间');
  129. return $show;
  130. }
  131. /**
  132. * Make a form builder.
  133. *
  134. * @return Form
  135. */
  136. protected function form()
  137. {
  138. $form = new Form(new Category);
  139. $form->display('ID');
  140. $form->display('添加时间');
  141. $form->display('更新时间');
  142. return $form;
  143. }
  144. /**
  145. * Make a form builder.
  146. *
  147. * @return Form
  148. */
  149. protected function editForm($id)
  150. {
  151. $form = new Form(new Category);
  152. $category = Category::find($id);
  153. $form->text('demand', '需求')->rules([
  154. 'required',
  155. ])->setWidth(3)->setMustMark();
  156. $form->select('alias', '别名')->options(
  157. CategoryGroups::List()->pluck('alias', 'alias')
  158. )->load('category_name', route('admin.sys.cateTrade'))->rules([
  159. 'required',
  160. ])->setWidth(3)->setMustMark();
  161. $form->select('category_name', '名称')->options(CategoryGroups::where('alias', $category->alias)->pluck('name','name'))->rules([
  162. 'required',
  163. ])->setWidth(3)->setMustMark();
  164. $form->number('order', '排序')->min(0)->default(0);
  165. return $form;
  166. }
  167. protected function createForm()
  168. {
  169. $form = new Form(new Category);
  170. $form->text('demand', '需求')->rules([
  171. 'required',
  172. ])->help('月薪分类:1000~1500/月,10000以上/月')->setWidth(3)->setMustMark();
  173. $form->select('alias', '别名')->options(
  174. CategoryGroups::List()->pluck('alias', 'alias')
  175. )->load('category_name', route('admin.sys.cateTrade'))->rules([
  176. 'required',
  177. ])->setWidth(3)->setMustMark();
  178. $form->select('category_name', '名称')->rules([
  179. 'required',
  180. ])->setWidth(3)->setMustMark();
  181. $form->number('order', '排序')->min(0)->default(0);
  182. return $form;
  183. }
  184. /**
  185. * Store a newly created resource in storage.
  186. *
  187. * @return mixed
  188. */
  189. public function store()
  190. {
  191. return $this->createForm()->store();
  192. }
  193. /**
  194. * Update the specified resource in storage.
  195. *
  196. * @param int $id
  197. *
  198. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  199. */
  200. public function update($id)
  201. {
  202. return $this->editForm($id)->update($id);
  203. }
  204. }