您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用Laravel框架怎么實(shí)現(xiàn)一個(gè)學(xué)生信息管理平臺(tái),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
//這是路由文件的關(guān)鍵代碼 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']); });
//控制器文件的關(guān)鍵代碼,增刪改查 class StudentController extends Controller{ //學(xué)生列表 public function index(){ $students = Student::paginate(20); //dd($students); return view('student.index',[ 'students'=>$students, ]); } //新增頁(yè)面 public function create(Request $request){ $student = new Student(); if($request->isMethod('POST')){ //1.控制器驗(yàn)證 /*$this->validate($request,[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項(xiàng)', 'min'=>':attribute長(zhǎng)度不符合要求', 'integer'=>':attribute必須為整數(shù)', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]);*/ //2.Validator類驗(yàn)證 $validator = \Validator::make($request->input(),[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項(xiàng)', 'min'=>':attribute長(zhǎng)度不符合要求', 'integer'=>':attribute必須為整數(shù)', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]); //withInput保持?jǐn)?shù)據(jù) 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, ]); } //保存數(shù)據(jù)操作 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(); } } //更新數(shù)據(jù)操作 public function update(Request $request,$id){ $student = Student::find($id); if($request->isMethod('POST')){ //Validator類驗(yàn)證 $validator = \Validator::make($request->input(),[ 'Student.name'=>'required|min:2|max:20', 'Student.age' =>'required|integer', 'Student.sex' =>'required|integer', ],[ 'required'=>':attribute 為必填項(xiàng)', 'min'=>':attribute長(zhǎng)度不符合要求', 'integer'=>':attribute必須為整數(shù)', ],[ 'Student.name'=>'姓名', 'Student.age' =>'年齡', 'Student.sex' =>'性別' ]); //withInput保持?jǐn)?shù)據(jù) 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); } } }
下面是效果展示
學(xué)生列表頁(yè)
新增頁(yè)面
詳情頁(yè)面
修改頁(yè)面
上述就是小編為大家分享的使用Laravel框架怎么實(shí)現(xiàn)一個(gè)學(xué)生信息管理平臺(tái)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。