Laravel框架實現(xiàn)簡單的學(xué)生信息管理平臺案例【附源碼下載】
本文實例講述了Laravel框架實現(xiàn)簡單的學(xué)生信息管理平臺。分享給大家供大家參考,具體如下:
laravel框架寫的簡易版的學(xué)生信息管理平臺,貫穿了laravel的控制器、視圖、模板、模型、中間件、路由規(guī)則的使用。
頁面是使用BootStrap前端框架搭建
使用laravel實現(xiàn)了增刪改查的功能。
代碼下載鏈接在文章底部。
//這是路由文件的關(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,
]);
}
//新增頁面
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必須為整數(shù)',
],[
'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必須為整數(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類驗證
$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必須為整數(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é)生列表頁

新增頁面

詳情頁面

修改頁面


完整實例代碼點擊此處本站下載。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
- Laravel框架實現(xiàn)的rbac權(quán)限管理操作示例
- Laravel5權(quán)限管理方法詳解
- Laravel 5 框架入門(二)構(gòu)建 Pages 的管理功能
- Laravel5.0+框架郵件發(fā)送功能實現(xiàn)方法圖文與實例詳解
- Laravel框架集成UEditor編輯器的方法圖文與實例詳解
- Laravel如何創(chuàng)建服務(wù)器提供者實例代碼
- Laravel框架自定義驗證過程實例分析
- laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解
- Laravel接收前端ajax傳來的數(shù)據(jù)的實例代碼
- 在Laravel框架里實現(xiàn)發(fā)送郵件實例(郵箱驗證)
- Laravel路由設(shè)定和子路由設(shè)定實例分析
相關(guān)文章
在laravel中使用with實現(xiàn)動態(tài)添加where條件
今天小編就為大家分享一篇在laravel中使用with實現(xiàn)動態(tài)添加where條件,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Yii2框架中使用PHPExcel導(dǎo)出Excel文件的示例
本篇文章主要介紹了Yii2框架中使用PHPExcel導(dǎo)出Excel文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
php 提速工具eAccelerator 配置參數(shù)詳解
php 提速工具eAccelerator 配置參數(shù)詳解,需要的朋友可以參考下。2010-05-05
Laravel5.6框架使用CKEditor5相關(guān)配置詳解
這篇文章主要介紹了Laravel5.6框架使用CKEditor5相關(guān)配置,結(jié)合實例形式詳細(xì)分析了Laravel5.6框架整合CKEditor5編輯器相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-07-07
laravel 框架結(jié)合關(guān)聯(lián)查詢 when()用法分析
這篇文章主要介紹了laravel 框架結(jié)合關(guān)聯(lián)查詢 when()用法,結(jié)合實例形式分析了laravel5.6框架when()基本原理、使用方法及操作注意事項,需要的朋友可以參考下2019-11-11

