StudentController.php 6.3 KB

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