QuestionController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Model\Question;
  4. use App\Http\Controllers\Controller;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. use App\Model\Option;
  12. class QuestionController 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('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('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('description')
  54. ->row($this->form()->edit($id))
  55. ->row(Admin::grid(Option::class, function (Grid $grid) use ($id) {
  56. $grid->setName('options')
  57. ->setTitle('选项')
  58. ->setRelation(Question::find($id)->options())
  59. ->resource('/admin/option');
  60. $grid->option_id('选项ID');
  61. $grid->title('标题');
  62. $grid->is_answer('是否答案');
  63. $grid->created_at('创建时间');
  64. $grid->updated_at('更新时间');
  65. }));
  66. }
  67. /**
  68. * Create interface.
  69. *
  70. * @param Content $content
  71. * @return Content
  72. */
  73. public function create(Content $content)
  74. {
  75. return $content
  76. ->header('创建')
  77. ->description('description')
  78. ->body($this->form());
  79. }
  80. /**
  81. * Make a grid builder.
  82. *
  83. * @return Grid
  84. */
  85. protected function grid()
  86. {
  87. $grid = new Grid(new Question);
  88. $grid->question_id('问题ID');
  89. $grid->title('标题');
  90. $grid->is_released('是否发布');
  91. $grid->created_at('创建时间');
  92. $grid->updated_at('更新时间');
  93. $grid->deleted_at('删除时间');
  94. return $grid;
  95. }
  96. /**
  97. * Make a show builder.
  98. *
  99. * @param mixed $id
  100. * @return Show
  101. */
  102. protected function detail($id)
  103. {
  104. $show = new Show(Question::findOrFail($id));
  105. $show->question_id('问题ID');
  106. $show->title('标题');
  107. $show->is_released('是否发布');
  108. $show->created_at('创建时间');
  109. $show->updated_at('更新时间');
  110. $show->deleted_at('删除时间');
  111. return $show;
  112. }
  113. /**
  114. * Make a form builder.
  115. *
  116. * @return Form
  117. */
  118. protected function form()
  119. {
  120. $form = new Form(new Question);
  121. $form->display('question_id', '问题ID');
  122. $form->text('title', '标题');
  123. $states = [
  124. 'on' => ['value' => 1, 'text' => '发布', 'color' => 'success'],
  125. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
  126. ];
  127. $form->switch('is_released', '是否发布')->states($states);
  128. return $form;
  129. }
  130. }