StudentController.php 5.3 KB

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