SpecialistColumnSecondController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use Encore\Admin\Controllers\AdminController;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Grid;
  6. use Encore\Admin\Show;
  7. use App\Models\SpecialistCloumn;
  8. use Illuminate\Http\Request;
  9. class SpecialistColumnSecondController extends AdminController
  10. {
  11. /**
  12. * Title for current resource.
  13. *
  14. * @var string
  15. */
  16. protected $title = '健康课堂-二级栏目管理';
  17. /**
  18. * Make a grid builder.
  19. *
  20. * @return Grid
  21. */
  22. protected function grid()
  23. {
  24. $grid = new Grid(new SpecialistCloumn);
  25. $grid->column('id', __('ID'))->sortable();
  26. $grid->column('pid', __('上级栏目'))->display(function ($pid) {
  27. return $pid > 0 ? SpecialistCloumn::find($pid)->name : '';
  28. });
  29. $grid->column('name', __('名称'));
  30. $grid->column('is_show', __('是否显示'))->display(function ($isShow) {
  31. return SpecialistCloumn::$isShowMap[$isShow];
  32. });
  33. $grid->column('rank', __('排序(越大越靠前)'))->sortable();
  34. $grid->column('created_at', __('创建时间'));
  35. $grid->column('updated_at', __('更新时间'));
  36. $grid->model()->where('pid', '>', SpecialistCloumn::PID_0);
  37. //过滤用到的一级栏目
  38. $cloumns = SpecialistCloumn::where('pid', 0)->get();
  39. $firstCloumns = [];
  40. foreach ($cloumns as $cloumn){
  41. $firstCloumns[$cloumn->id] = $cloumn->name;
  42. }
  43. // 过滤
  44. $grid->filter(function ($filter) use($firstCloumns) {
  45. $filter->like('name', '名称');
  46. $filter->equal('pid', '类别')->select($firstCloumns);
  47. $filter->equal('is_show', '是否显示')->radio(SpecialistCloumn::$isShowMap);
  48. });
  49. return $grid;
  50. }
  51. /**
  52. * Make a show builder.
  53. *
  54. * @param mixed $id
  55. * @return Show
  56. */
  57. protected function detail($id)
  58. {
  59. $show = new Show(SpecialistCloumn::findOrFail($id));
  60. $show->field('id', __('ID'));
  61. $show->field('created_at', __('Created at'));
  62. $show->field('updated_at', __('Updated at'));
  63. return $show;
  64. }
  65. /**
  66. * Make a form builder.
  67. *
  68. * @return Form
  69. */
  70. protected function form()
  71. {
  72. $form = new Form(new SpecialistCloumn);
  73. $form->display('id', __('ID'));
  74. //只显示一级栏目
  75. $cloumns = SpecialistCloumn::where('pid', 0)->get();
  76. $firstCloumns = [];
  77. foreach ($cloumns as $cloumn){
  78. $firstCloumns[$cloumn->id] = $cloumn->name;
  79. }
  80. $form->select('pid', __('上一级'))->options($firstCloumns)->rules('required');;
  81. $form->text('name', __('名称'))->rules('required');
  82. $form->image('pic', __('图片'))->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
  83. $form->text('url', __('链接地址'));
  84. $states = [
  85. 'on' => ['value' => 1, 'text' => '显示', 'color' => 'success'],
  86. 'off' => ['value' => 0, 'text' => '不显示', 'color' => 'danger'],
  87. ];
  88. $form->switch('is_show', __('是否显示'))->states($states);
  89. $form->number('rank', __('排序(越大越靠前)'))->default(0);
  90. return $form;
  91. }
  92. protected function secondCloumn(Request $request){
  93. $pid = $request->get('q');
  94. $result = SpecialistCloumn::where('pid', $pid)->get(['id', 'name as text']);
  95. return count($result) == 0 ? [['id'=>0,'text'=>'无']] : $result;
  96. }
  97. }