StudentController.php 4.9 KB

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