StudentController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  45. $list = $this->paginate($list);
  46. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  47. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  48. }
  49. public function create(Request $request)
  50. {
  51. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  52. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  53. }
  54. public function store(Request $request)
  55. {
  56. if(!$request->isMethod('POST')) {
  57. return $this->showWarning('访问错误');
  58. }
  59. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  60. return $this->showWarning('数据错误');
  61. }
  62. $validator = Validator::make($request->input('data'), [
  63. 'phone' => 'required|unique:students'
  64. ], [
  65. 'phone.required' => '手机必填',
  66. 'phone.unique' => '手机已存在',
  67. ]);
  68. if($validator->fails()) {
  69. return back()->withErrors($validator)->withInput();
  70. }
  71. $res = $this->model->create($request->input('data'));
  72. if(!$res) {
  73. return $this->showWarning('数据库保存失败!');
  74. }
  75. return $this->showMessage('操作成功', $this->redirect_index);
  76. }
  77. public function edit(Request $request)
  78. {
  79. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  80. return $this->showWarning('数据错误!');
  81. }
  82. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  83. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  84. }
  85. public function update(Request $request)
  86. {
  87. if(!$request->isMethod('POST')) {
  88. return $this->showWarning('访问错误');
  89. }
  90. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  91. return $this->showWarning('数据错误');
  92. }
  93. $validator = Validator::make($request->input('data'), [
  94. 'phone' => 'required'
  95. ], [
  96. 'phone.required' => '手机必填'
  97. ]);
  98. if($validator->fails()) {
  99. return back()->withErrors($validator)->withInput();
  100. }
  101. $tmp = $this->model->where([
  102. ['id', '<>', $request->input('id')],
  103. ['phone', '=', $request->input('data')['phone']],
  104. ])->first();
  105. if(!empty($tmp)) {
  106. $validator->errors()->add('phone', '手机已存在!');
  107. return back()->withErrors($validator)->withInput();
  108. }
  109. $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
  110. if(!$res) {
  111. return $this->showWarning('数据库保存失败!');
  112. }
  113. return $this->showMessage('操作成功', $this->redirect_index);
  114. }
  115. public function delete(Request $request)
  116. {
  117. if(!$request->isMethod('POST')) {
  118. return $this->showWarning('访问错误');
  119. }
  120. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  121. return $this->showWarning('访问错误');
  122. }
  123. StudentCourse::where('student_id', $item->id)->delete();
  124. StudentCourseTeacher::where('student_id', $item->id)->delete();
  125. $res = $item->delete();
  126. if(!$res) {
  127. return $this->showWarning('数据库删除失败');
  128. }
  129. return $this->showMessage('操作成功');
  130. }
  131. }