StudentController.php 3.7 KB

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