TeacherController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Course;
  4. use App\Models\RemarkDetail;
  5. use App\Models\Teacher;
  6. use App\Models\TeacherCourse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Validator;
  9. class TeacherController extends Controller
  10. {
  11. protected $redirect_index = '/admin/Teacher/index';
  12. protected $view_path = 'admin.teachers.';
  13. protected $pre_uri = '/admin/Teacher/';
  14. protected $model_name = '讲师';
  15. protected $model;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->model = new Teacher();
  20. }
  21. public function index(Request $request)
  22. {
  23. $list = $this->model->where('id', '>', 0)->orderBy('created_at', 'desc');
  24. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  25. $keyword = '%' . trim($request->input('keyword')) . '%';
  26. $list = $list->where('name', 'like', $keyword);
  27. }
  28. $list = $list->paginate()->withPath($this->getPaginateUrl());
  29. foreach($list as $item) {
  30. $item->average_score = RemarkDetail::where('remark_id', $item->id)->get()->avg('score');
  31. $item->average_score = round($item->average_score, 1);
  32. }
  33. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  34. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  35. }
  36. public function create(Request $request)
  37. {
  38. $courses = Course::all();
  39. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  40. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  41. }
  42. public function store(Request $request)
  43. {
  44. if(!$request->isMethod('POST')) {
  45. return $this->showWarning('访问错误');
  46. }
  47. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  48. return $this->showWarning('数据错误');
  49. }
  50. $validator = Validator::make($request->input('data'), [
  51. 'phone' => 'required|unique:teachers',
  52. 'password' => 'required|min:6',
  53. ], [
  54. 'phone.required' => '手机必填',
  55. 'phone.unique' => '手机已存在',
  56. 'password.required' => '密码必填',
  57. 'password.min' => '密码不少于6位',
  58. ]);
  59. if($validator->fails()) {
  60. return back()->withErrors($validator)->withInput();
  61. }
  62. $data = $request->input('data');
  63. $data['password'] = bcrypt($data['password']);
  64. $res = $this->model->create($data);
  65. if(!$res) {
  66. return $this->showWarning('数据库保存失败!');
  67. }
  68. if(!empty($request->input('courses'))) {
  69. TeacherCourse::where('teacher_id', $res->id)->delete();
  70. foreach($request->input('courses') as $item) {
  71. TeacherCourse::create([
  72. 'teacher_id' => $res->id,
  73. 'course_id' => $item
  74. ]);
  75. }
  76. }
  77. return $this->showMessage('操作成功', $this->redirect_index);
  78. }
  79. public function edit(Request $request)
  80. {
  81. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  82. return $this->showWarning('数据错误!');
  83. }
  84. $courses = Course::all();
  85. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  86. $ids_string = $item->courses->implode('id', ',');
  87. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model', 'courses', 'ids_string'));
  88. }
  89. public function update(Request $request)
  90. {
  91. if(!$request->isMethod('POST')) {
  92. return $this->showWarning('访问错误');
  93. }
  94. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  95. return $this->showWarning('数据错误');
  96. }
  97. $validator = Validator::make($request->input('data'), [
  98. 'phone' => 'required'
  99. ], [
  100. 'phone.required' => '手机必填'
  101. ]);
  102. if($validator->fails()) {
  103. return back()->withErrors($validator)->withInput();
  104. }
  105. $data = $request->input('data');
  106. $tmp = $this->model->where([
  107. ['phone', '=', $data['phone']],
  108. ['id', '<>', $request->input('id')],
  109. ])->first();
  110. if(!empty($tmp)) {
  111. $validator->errors()->add('phone', '手机号已存在');
  112. return back()->withErrors($validator)->withInput();
  113. }
  114. if($request->has('is_update_password') && $request->input('is_update_password') == 1) {
  115. $data['password'] = bcrypt($request->input('password', 123456));
  116. }
  117. $res = $this->model->where('id', $request->input('id'))->update($data);
  118. if(!$res) {
  119. return $this->showWarning('数据库保存失败!');
  120. }
  121. $teacher = $this->model->find($request->input('id'));
  122. if(empty($teacher)) {
  123. return $this->showWarning('找不到讲师!');
  124. }
  125. if(!empty($request->input('courses'))) {
  126. TeacherCourse::where('teacher_id', $teacher->id)->delete();
  127. foreach($request->input('courses') as $item) {
  128. TeacherCourse::create([
  129. 'teacher_id' => $teacher->id,
  130. 'course_id' => $item
  131. ]);
  132. }
  133. }
  134. return $this->showMessage('操作成功', $this->redirect_index);
  135. }
  136. public function delete(Request $request)
  137. {
  138. if(!$request->isMethod('POST')) {
  139. return $this->showWarning('访问错误');
  140. }
  141. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  142. return $this->showWarning('访问错误');
  143. }
  144. TeacherCourse::where('teacher_id', $item->id)->delete();
  145. $res = $item->delete();
  146. if(!$res) {
  147. return $this->showWarning('数据库删除失败');
  148. }
  149. return $this->showMessage('操作成功');
  150. }
  151. }