Laravel框架实现简单的学生信息管理平台案例【附源码下载】
程序员文章站
2022-05-25 21:53:03
本文实例讲述了laravel框架实现简单的学生信息管理平台。分享给大家供大家参考,具体如下:
laravel框架写的简易版的学生信息管理平台,贯穿了laravel的控制器...
本文实例讲述了laravel框架实现简单的学生信息管理平台。分享给大家供大家参考,具体如下:
laravel框架写的简易版的学生信息管理平台,贯穿了laravel的控制器、视图、模板、模型、中间件、路由规则的使用。
页面是使用bootstrap前端框架搭建
使用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相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于laravel框架的php程序设计有所帮助。
上一篇: vue 父组件调用子组件方法及事件
下一篇: Laravel框架路由管理简单示例