ClockedController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Clocked;
  4. use App\Grade;
  5. use App\Http\Controllers\Controller;
  6. use App\School;
  7. use App\Sclass;
  8. use App\User;
  9. use Encore\Admin\Controllers\HasResourceActions;
  10. use Encore\Admin\Form;
  11. use Encore\Admin\Grid;
  12. use Encore\Admin\Layout\Content;
  13. use Encore\Admin\Show;
  14. class ClockedController extends Controller
  15. {
  16. use HasResourceActions;
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('Index')
  27. ->description('description')
  28. ->body($this->grid());
  29. }
  30. /**
  31. * Show interface.
  32. *
  33. * @param mixed $id
  34. * @param Content $content
  35. * @return Content
  36. */
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->header('Detail')
  41. ->description('description')
  42. ->body($this->detail($id));
  43. }
  44. /**
  45. * Edit interface.
  46. *
  47. * @param mixed $id
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function edit($id, Content $content)
  52. {
  53. return $content
  54. ->header('Edit')
  55. ->description('description')
  56. ->body($this->form()->edit($id));
  57. }
  58. /**
  59. * Create interface.
  60. *
  61. * @param Content $content
  62. * @return Content
  63. */
  64. public function create(Content $content)
  65. {
  66. return $content
  67. ->header('Create')
  68. ->description('description')
  69. ->body($this->form());
  70. }
  71. /**
  72. * Make a grid builder.
  73. *
  74. * @return Grid
  75. */
  76. protected function grid()
  77. {
  78. $grid = new Grid(new Clocked);
  79. $grid->id('编号');
  80. $grid->filter(function ($filter) {
  81. // 去掉默认的id过滤器
  82. $filter->disableIdFilter();
  83. // 在这里添加字段过滤器
  84. $filter->where(function ($query) {
  85. $query->whereHas('user', function ($query) {
  86. $query->where('name', 'like', "%{$this->input}%");
  87. });
  88. }, '姓名');
  89. $filter->where(function ($query) {
  90. $query->whereHas('user', function ($query) {
  91. $query->where('sclass_id', 'like', "%{$this->input}%");
  92. });
  93. }, '班级')->select('/admin/api/getSchoolSclass');
  94. });
  95. $grid->user_name('用户编号')->display(function () {
  96. $name = User::where('id', $this->user_id)->select('name')->first();
  97. if ($name) {
  98. return $name['name'];
  99. }
  100. return null;
  101. });
  102. $grid->school("学校班级信息")->display(function () {
  103. $name1 = User::where('id', $this->user_id)->select("school_id", "class_id", "grade_id")->first();
  104. if ($name1) {
  105. $school_name = School::where("id", $name1['school_id'])->select('name')->first();
  106. if ($school_name) {
  107. $school_name = $school_name['name'];
  108. }
  109. $grade = Grade::where("id", $name1['grade_id'])->select('name')->first();
  110. if ($grade) {
  111. $grade = $grade['name'];
  112. }
  113. $sclass = Sclass::where("id", $name1['sclass_id'])->select('name')->first();
  114. if ($sclass) {
  115. $sclass = $sclass['name'];
  116. }
  117. return $school_name . '-' . $grade . '-' . $sclass;
  118. }
  119. return null;
  120. });
  121. $grid->answerOK('答题结果')->display(function () {
  122. if ($this->answer) {
  123. return "正确";
  124. }
  125. return "错误";
  126. });
  127. $grid->created_at('打卡时间');
  128. return $grid;
  129. }
  130. /**
  131. * Make a show builder.
  132. *
  133. * @param mixed $id
  134. * @return Show
  135. */
  136. protected function detail($id)
  137. {
  138. $show = new Show(Clocked::findOrFail($id));
  139. $show->id('编号');
  140. $show->user_id('用户编号');
  141. $show->question_id('题目编号');
  142. $show->answer('答题结果');
  143. $show->created_at('创建时间');
  144. $show->updated_at('更新时间');
  145. return $show;
  146. }
  147. /**
  148. * Make a form builder.
  149. *
  150. * @return Form
  151. */
  152. protected function form()
  153. {
  154. $form = new Form(new Clocked);
  155. $form->number('user_id', '用户编号');
  156. $form->number('question_id', '题目编号');
  157. $form->switch('answer', '答案');
  158. return $form;
  159. }
  160. }