Laravel框架实现简单的学生信息管理平台案例【附

编程学习 2021-07-04 23:24www.dzhlxh.cn编程入门
这篇文章主要介绍了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);
    }
  }
}

狼蚁网站SEO优化是效果展示

学生列表页

新增页面

详情页面

修改页面

完整实例代码点击此处。

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板