123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Exports\StudentsExport;
- use App\Imports\StudentsImport;
- use App\Models\Course;
- use App\Models\Student;
- use App\Models\StudentCourse;
- use App\Models\StudentCourseTeacher;
- use App\Models\Teacher;
- use App\Models\TeacherStudent;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use Maatwebsite\Excel\Facades\Excel;
- class StudentController extends Controller
- {
- protected $redirect_index = '/admin/Student/index';
- protected $view_path = 'admin.students.';
- protected $pre_uri = '/admin/Student/';
- protected $model_name = '学员';
- protected $model;
- public function __construct()
- {
- parent::__construct();
- $this->model = new Student();
- }
- public function index(Request $request)
- {
- $list = $this->model->where('id', '>', 0)->orderBy('created_at', 'desc');
- if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
- $keyword = '%' . trim($request->input('keyword')) . '%';
- $list = $list->where('name', 'like', $keyword);
- }
- $list = $list->get();
- foreach($list as $item) {
- $tmp = $item->getCourseInfo();
- $item->course_name = $tmp['course_name'];
- $item->apply_date = $tmp['apply_date'];
- $item->end_date = $tmp['end_date'];
- $item->teacher_names = $tmp['teacher_names'];
- }
- if($request->has('sort_field') && $request->has('sort_field_by')) {
- if($request->input('sort_field_by') == 'asc') {
- $list = $list->sortBy($request->input('sort_field'));
- } else {
- $list = $list->sortByDesc($request->input('sort_field'));
- }
- }
- $list = $this->paginate($list);
- list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
- return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
- }
- public function create(Request $request)
- {
- list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
- return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers'));
- }
- public function store(Request $request)
- {
- if(!$request->isMethod('POST')) {
- return $this->showWarning('访问错误');
- }
- if(empty($request->input('data')) || !is_array($request->input('data'))) {
- return $this->showWarning('数据错误');
- }
- $validator = Validator::make($request->input('data'), [
- 'phone' => 'required|unique:students'
- ], [
- 'phone.required' => '手机必填',
- 'phone.unique' => '手机已存在',
- ]);
- if($validator->fails()) {
- return back()->withErrors($validator)->withInput();
- }
- $res = $this->model->create($request->input('data'));
- if(!$res) {
- return $this->showWarning('数据库保存失败!');
- }
- return $this->showMessage('操作成功', $this->redirect_index);
- }
- public function edit(Request $request)
- {
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
- return $this->showWarning('数据错误!');
- }
- list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
- return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
- }
- public function detail(Request $request)
- {
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
- return $this->showWarning('数据错误!');
- }
- list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
- return view($this->view_path . 'detail', compact('item','pre_uri', 'model_name', 'model'));
- }
- public function update(Request $request)
- {
- if(!$request->isMethod('POST')) {
- return $this->showWarning('访问错误');
- }
- if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
- return $this->showWarning('数据错误');
- }
- $validator = Validator::make($request->input('data'), [
- 'phone' => 'required'
- ], [
- 'phone.required' => '手机必填'
- ]);
- if($validator->fails()) {
- return back()->withErrors($validator)->withInput();
- }
- $tmp = $this->model->where([
- ['id', '<>', $request->input('id')],
- ['phone', '=', $request->input('data')['phone']],
- ])->first();
- if(!empty($tmp)) {
- $validator->errors()->add('phone', '手机已存在!');
- return back()->withErrors($validator)->withInput();
- }
- $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
- if(!$res) {
- return $this->showWarning('数据库保存失败!');
- }
- return $this->showMessage('操作成功', $this->redirect_index);
- }
- public function delete(Request $request)
- {
- if(!$request->isMethod('POST')) {
- return $this->showWarning('访问错误');
- }
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
- return $this->showWarning('访问错误');
- }
- StudentCourse::where('student_id', $item->id)->delete();
- StudentCourseTeacher::where('student_id', $item->id)->delete();
- $res = $item->delete();
- if(!$res) {
- return $this->showWarning('数据库删除失败');
- }
- return $this->showMessage('操作成功');
- }
-
- public function export(Request $request)
- {
- return Excel::download(new StudentsExport(), '学员.xlsx');
- }
- public function import(Request $request)
- {
- if(!$request->has('file') || !$request->file('file')) {
- return $this->showWarning('请选择导入的文件');
- }
- Excel::import(new StudentsImport(), $request->file('file'));
- return $this->showMessage('操作成功');
- }
- }
|