StudentController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class StudentController extends Controller
  12. {
  13. protected $redirect_index = '/teacher/Student/index';
  14. protected $view_path = 'teacher.students.';
  15. protected $pre_uri = '/teacher/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. $teacher = Auth::guard('teacher')->user();
  26. $ids = StudentCourseTeacher::where('teacher_id', $teacher->id)->orWhere('teacher_id', 0)->pluck('student_id');
  27. $list = $this->model->whereIn('id', $ids)->orderBy('created_at', 'desc');
  28. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  29. $keyword = '%' . trim($request->input('keyword')) . '%';
  30. $list = $list->where('name', 'like', $keyword);
  31. }
  32. $list = $list->paginate()->withPath($this->getPaginateUrl());
  33. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  34. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  35. }
  36. public function create(Request $request)
  37. {
  38. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  39. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  40. }
  41. public function store(Request $request)
  42. {
  43. if(!$request->isMethod('POST')) {
  44. return $this->showWarning('访问错误');
  45. }
  46. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  47. return $this->showWarning('数据错误');
  48. }
  49. $res = $this->model->create($request->input('data'));
  50. if(!$res) {
  51. return $this->showWarning('数据库保存失败!');
  52. }
  53. return $this->showMessage('操作成功', $this->redirect_index);
  54. }
  55. public function edit(Request $request)
  56. {
  57. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  58. return $this->showWarning('数据错误!');
  59. }
  60. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  61. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  62. }
  63. public function update(Request $request)
  64. {
  65. if(!$request->isMethod('POST')) {
  66. return $this->showWarning('访问错误');
  67. }
  68. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  69. return $this->showWarning('数据错误');
  70. }
  71. $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
  72. if(!$res) {
  73. return $this->showWarning('数据库保存失败!');
  74. }
  75. return $this->showMessage('操作成功', $this->redirect_index);
  76. }
  77. public function delete(Request $request)
  78. {
  79. if(!$request->isMethod('POST')) {
  80. return $this->showWarning('访问错误');
  81. }
  82. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  83. return $this->showWarning('访问错误');
  84. }
  85. StudentCourse::where('student_id', $item->id)->delete();
  86. StudentCourseTeacher::where('student_id', $item->id)->delete();
  87. $res = $item->delete();
  88. if(!$res) {
  89. return $this->showWarning('数据库删除失败');
  90. }
  91. return $this->showMessage('操作成功');
  92. }
  93. }