StudentController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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->paginate()->withPath($this->getPaginateUrl());
  31. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  32. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  33. }
  34. public function create(Request $request)
  35. {
  36. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  37. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  38. }
  39. public function store(Request $request)
  40. {
  41. if(!$request->isMethod('POST')) {
  42. return $this->showWarning('访问错误');
  43. }
  44. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  45. return $this->showWarning('数据错误');
  46. }
  47. $validator = Validator::make($request->input('data'), [
  48. 'phone' => 'required|unique:students'
  49. ], [
  50. 'phone.required' => '手机必填',
  51. 'phone.unique' => '手机已存在',
  52. ]);
  53. if($validator->fails()) {
  54. return back()->withErrors($validator);
  55. }
  56. $res = $this->model->create($request->input('data'));
  57. if(!$res) {
  58. return $this->showWarning('数据库保存失败!');
  59. }
  60. return $this->showMessage('操作成功', $this->redirect_index);
  61. }
  62. public function edit(Request $request)
  63. {
  64. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  65. return $this->showWarning('数据错误!');
  66. }
  67. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  68. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  69. }
  70. public function update(Request $request)
  71. {
  72. if(!$request->isMethod('POST')) {
  73. return $this->showWarning('访问错误');
  74. }
  75. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  76. return $this->showWarning('数据错误');
  77. }
  78. $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
  79. if(!$res) {
  80. return $this->showWarning('数据库保存失败!');
  81. }
  82. return $this->showMessage('操作成功', $this->redirect_index);
  83. }
  84. public function delete(Request $request)
  85. {
  86. if(!$request->isMethod('POST')) {
  87. return $this->showWarning('访问错误');
  88. }
  89. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  90. return $this->showWarning('访问错误');
  91. }
  92. StudentCourse::where('student_id', $item->id)->delete();
  93. StudentCourseTeacher::where('student_id', $item->id)->delete();
  94. $res = $item->delete();
  95. if(!$res) {
  96. return $this->showWarning('数据库删除失败');
  97. }
  98. return $this->showMessage('操作成功');
  99. }
  100. }