您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關使用Laravel框架怎么實現一個學生信息管理平臺,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
//這是路由文件的關鍵代碼 Route::group(['middleware' => ['web']], function () { Route::get('student/index',['uses'=>'StudentController@index']); Route::any('student/create',['uses'=>'StudentController@create']); Route::post('student/save',['uses'=>'StudentController@save']); Route::any('student/update/{id}',['uses'=>'StudentController@update']); Route::any('student/detail/{id}',['uses'=>'StudentController@detail']); Route::any('student/delete/{id}',['uses'=>'StudentController@delete']); });
//控制器文件的關鍵代碼,增刪改查 class StudentController extends Controller{ //學生列表 public function index(){ $students = Student::paginate(20); //dd($students); return view('student.index',[ 'students'=>$students, ]); } //新增頁面 public function create(Request $request){ $student = new Student(); if($request->isMethod('POST')){ //1.控制器驗證 /*$this->validate($request,[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項', 'min'=>':attribute長度不符合要求', 'integer'=>':attribute必須為整數', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]);*/ //2.Validator類驗證 $validator = \Validator::make($request->input(),[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項', 'min'=>':attribute長度不符合要求', 'integer'=>':attribute必須為整數', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]); //withInput保持數據 if($validator->fails()){ return redirect()->back()->withErrors($validator)->withInput(); } $data = $request->input('Student'); if(Student::create($data)){ return redirect('student/index')->with('success','添加成功'); }else{ return redirect()->back(); } } return view('student.create',[ 'student'=>$student, ]); } //保存數據操作 public function save(Request $request){ $data = $request->input('Student'); $student = new Student(); $student->name = $data['name']; $student->age = $data['age']; $student->sex = $data['sex']; if($student->save()){ return redirect('student/index'); }else{ return redirect()->back(); } } //更新數據操作 public function update(Request $request,$id){ $student = Student::find($id); if($request->isMethod('POST')){ //Validator類驗證 $validator = \Validator::make($request->input(),[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項', 'min'=>':attribute長度不符合要求', 'integer'=>':attribute必須為整數', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]); //withInput保持數據 if($validator->fails()){ return redirect()->back()->withErrors($validator)->withInput(); } $data = $request->input('Student'); $student->name = $data['name']; $student->age = $data['age']; $student->sex = $data['sex']; if($student->save()){ return redirect('student/index')->with('success','修改成功-'.$id); } } return view('student.update',[ 'student'=>$student, ]); } //信息詳情 public function detail($id){ $student = Student::find($id); return view('student.detail',[ 'student'=>$student, ]); } //刪除操作 public function delete($id){ $student = Student::find($id); if($student->delete()){ return redirect('student/index')->with('success','刪除成功-'.$id); }else{ return redirect('student/index')->with('error','刪除失敗-'.$id); } } }
下面是效果展示
學生列表頁
新增頁面
詳情頁面
修改頁面
上述就是小編為大家分享的使用Laravel框架怎么實現一個學生信息管理平臺了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。