• Laravel 增删改查 操作


    1 视图

     <td>
                            <a href="{{url('student/show',['id'=>$student->id])}}">详情</a>
                            <a href="{{url('student/update',['id'=>$student->id])}}">修改</a>
                            <a href="{{url('student/destory',['id'=>$student->id])}}"
                               onclick="if(confirm('确认要删除吗')==false)return false"
                            >删除</a>
                        </td>

    2 路由

    //Laravel 自带了开箱即用的 web 和 api 两个中间件组已包含
    //可以应用到 Web UI 和 API 路由的通用中间件
    Route::group(['middleware'=>['web']],function (){
        Route::get('student/index',['uses'=>'StudentController@index']);
        Route::any('student/create',['uses'=>'StudentController@create']);
        Route::any('student/save',['uses'=>'StudentController@save']);
        Route::any('student/update/{id}',['uses'=>'StudentController@update']);
        Route::any('student/show/{id}',['uses'=>'StudentController@show']);
        Route::any('student/destory/{id}',['uses'=>'StudentController@destory']);
    });

    3 控制器

        //学生列表页
        public function index()
        {
            //(每页显示的个数)  //$students = Student::get();
            $students = Student::paginate(5);
    /*        foreach ($students as $student){
                dd($student->gender());
            }*/
            $data['students'] = $students;
            return view('student.index',$data);
        }
    
        //action=""
        public function create(Request $request)
        {
            if ($request->isMethod('POST')){
                //web中间件会自动捕获 保存到session中,并显示到view
                //1 控制器验证
               /* $this->validate($request,[
                    'Student.name'=>'required|min:2|max:20',
                    'Student.age'=>'required|integer',
                    'Student.gender'=>'required|integer',
                ],[
                    'required'=>':attribute 为必填项',
                    'min'=>':attribute 长度太短了',
                    'max'=>':attribute 长度太长了',
                    'integer'=>':attribute 必须为整数',
                ],[
                    'Student.name'=>'姓名',
                    'Student.age'=>'年龄',
                    'Student.gender'=>'性别',
    
                ]);*/
                //2 Validator类验证
                $validdator = Validator::make($request->input(),[
                    'Student.name'=>'required|min:2|max:20',
                    'Student.age'=>'required|integer',
                    'Student.gender'=>'required|integer',
                ],[
                    'required'=>':attribute 为必填项',
                    'min'=>':attribute 输入长度太短了',
                    'max'=>':attribute 输入长度太长了',
                    'integer'=>':attribute 必须为整数',
                ],[
                    'Student.name'=>'姓名',
                    'Student.age'=>'年龄',
                    'Student.gender'=>'性别',
    
                ]);
                if ($validdator->fails()){
                    return redirect()
                        ->back()
                        ->withErrors($validdator)
                        ->withInput();//数据保持
                }
    
                $data = $request->input('Student');
                if (Student::create($data)){
                    return redirect('student/index')->with('success','添加成功');
                }
            }
            $student = new Student();
            return view('student.create',['student'=>$student]);
        }
    
    
        //action="/student/save"
        public function save(Request $request)
        {
            $data = $request->input('Student');
            $student = new Student();
            $student->name = $data['name'];
    
            if ($student->save()){
                return redirect('student/index');
            }
            //return redirect('student/create');
            return redirect()->back();
        }
    
        public  function  update(Request $request, $id)
        {
            $student = Student::find($id);
    
            if ($request->isMethod('POST')){
                $data = $request->input('Student');
    
                $validdator = Validator::make($request->input(),[
                    'Student.name'=>'required|min:2|max:20',
                    'Student.age'=>'required|integer',
                    'Student.gender'=>'required|integer',
                ],[
                    'required'=>':attribute 为必填项',
                    'min'=>':attribute 输入长度太短了',
                    'max'=>':attribute 输入长度太长了',
                    'integer'=>':attribute 必须为整数',
                ],[
                    'Student.name'=>'姓名',
                    'Student.age'=>'年龄',
                    'Student.gender'=>'性别',
    
                ]);
                if ($validdator->fails()){
                    return redirect()
                        ->back()
                        ->withErrors($validdator)
                        ->withInput();//数据保持
                }
    
    
                $student->name      = $data['name'];
                $student->age       = $data['age'];
                $student->gender    = $data['gender'];
                if ($student->save()){
                    return redirect('student/index')
                        ->with('success','修改成功-'.$id);
                }
            }
    
            $data['student'] = $student;
            return view('student.update',$data);
        }
    
        public function show($id)
        {
            $student = Student::find($id);
            return view('student.show',[
                'student'=>$student
            ]);
    
        }
    
        public function destory($id)
        {
            $student = Student::find($id);
            if($student->delete()){
                return redirect('student/index')
                    ->with('success','删除成功-'.$id);
    
    
            }
            return redirect('student/index')
                ->with('error','删除失败-'.$id);
    
        }

    4 完整代码

    链接:https://pan.baidu.com/s/1hALthlpC_9D1FGXf4G_kyA
    提取码:k47r

     

  • 相关阅读:
    mybatis generator插件开发
    webserver实现
    Linux的selinux
    oracle从备份归档日志的方法集中回收
    百度地图 Android SDK
    Oracle实践--PL/SQL表分区的基础
    js插件---jquery给表格添加行列
    m_Orchestrate learning system---三十四、使用重定义了$的插件的时候最容易出现的问题是什么
    2018十大国产佳片
    javascript对象使用总结
  • 原文地址:https://www.cnblogs.com/polax/p/13358388.html
Copyright © 2020-2023  润新知