StudentController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers\Teacher;
  3. use App\Models\Course;
  4. use App\Models\Student;
  5. use App\Models\StudentCourse;
  6. use App\Models\StudentCourseTeacher;
  7. use App\Models\Teacher;
  8. use App\Models\TeacherStudent;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Validator;
  12. class StudentController extends Controller
  13. {
  14. protected $redirect_index = '/teacher/Student/index';
  15. protected $view_path = 'teacher.students.';
  16. protected $pre_uri = '/teacher/Student/';
  17. protected $model_name = '学员';
  18. protected $model;
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->model = new Student();
  23. }
  24. public function index(Request $request)
  25. {
  26. $teacher = Auth::guard('teacher')->user();
  27. $ids = StudentCourseTeacher::where('teacher_id', $teacher->id)->orWhere('teacher_id', 0)->pluck('student_id');
  28. $list = $this->model->whereIn('id', $ids)->orderBy('created_at', 'desc');
  29. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  30. $keyword = '%' . trim($request->input('keyword')) . '%';
  31. $list = $list->where('name', 'like', $keyword);
  32. }
  33. $list = $list->get();
  34. foreach($list as $item) {
  35. $tmp = $item->getCourseInfo();
  36. $item->course_name = $tmp['course_name'];
  37. $item->apply_date = $tmp['apply_date'];
  38. $item->end_date = $tmp['end_date'];
  39. $item->teacher_names = $tmp['teacher_names'];
  40. }
  41. if($request->has('sort_field') && $request->has('sort_field_by')) {
  42. if($request->input('sort_field_by') == 'asc') {
  43. $list = $list->sortBy($request->input('sort_field'));
  44. } else {
  45. $list = $list->sortByDesc($request->input('sort_field'));
  46. }
  47. }
  48. $list = $this->paginate($list);
  49. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  50. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  51. }
  52. public function create(Request $request)
  53. {
  54. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  55. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  56. }
  57. public function store(Request $request)
  58. {
  59. if(!$request->isMethod('POST')) {
  60. return $this->showWarning('访问错误');
  61. }
  62. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  63. return $this->showWarning('数据错误');
  64. }
  65. $validator = Validator::make($request->input('data'), [
  66. 'phone' => 'required|unique:students'
  67. ], [
  68. 'phone.required' => '手机必填',
  69. 'phone.unique' => '手机已存在',
  70. ]);
  71. if($validator->fails()) {
  72. return back()->withErrors($validator)->withInput();
  73. }
  74. $res = $this->model->create($request->input('data'));
  75. if(!$res) {
  76. return $this->showWarning('数据库保存失败!');
  77. }
  78. return $this->showMessage('操作成功', $this->redirect_index);
  79. }
  80. public function edit(Request $request)
  81. {
  82. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  83. return $this->showWarning('数据错误!');
  84. }
  85. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  86. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  87. }
  88. public function detail(Request $request)
  89. {
  90. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  91. return $this->showWarning('数据错误!');
  92. }
  93. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  94. return view($this->view_path . 'detail', compact('item','pre_uri', 'model_name', 'model'));
  95. }
  96. public function update(Request $request)
  97. {
  98. if(!$request->isMethod('POST')) {
  99. return $this->showWarning('访问错误');
  100. }
  101. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  102. return $this->showWarning('数据错误');
  103. }
  104. $validator = Validator::make($request->input('data'), [
  105. 'phone' => 'required'
  106. ], [
  107. 'phone.required' => '手机必填'
  108. ]);
  109. if($validator->fails()) {
  110. return back()->withErrors($validator)->withInput();
  111. }
  112. $tmp = $this->model->where([
  113. ['id', '<>', $request->input('id')],
  114. ['phone', '=', $request->input('data')['phone']],
  115. ])->first();
  116. if(!empty($tmp)) {
  117. $validator->errors()->add('phone', '手机已存在!');
  118. return back()->withErrors($validator)->withInput();
  119. }
  120. $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
  121. if(!$res) {
  122. return $this->showWarning('数据库保存失败!');
  123. }
  124. return $this->showMessage('操作成功', $this->redirect_index);
  125. }
  126. public function delete(Request $request)
  127. {
  128. if(!$request->isMethod('POST')) {
  129. return $this->showWarning('访问错误');
  130. }
  131. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  132. return $this->showWarning('访问错误');
  133. }
  134. StudentCourse::where('student_id', $item->id)->delete();
  135. StudentCourseTeacher::where('student_id', $item->id)->delete();
  136. $res = $item->delete();
  137. if(!$res) {
  138. return $this->showWarning('数据库删除失败');
  139. }
  140. return $this->showMessage('操作成功');
  141. }
  142. }