1控制器验证 $this->validate($request,[ ‘Student.name‘=>‘required|min:2|max:20‘, ‘Student.age‘=>‘required|integer‘, ‘Student.sex‘=>‘required|integer‘, ],[ ‘required‘=>‘:attribute 为必填项目‘, ‘min‘=>‘:attribute 长度必须符合要求‘, 占位符 ],[ ‘Student.name‘=>‘姓名‘, ‘Student.age‘=>‘年龄‘, ‘Student.sex‘=>‘性别‘, ]); 2Validator类验证
$val= \Validator::make($request->input(),[ ‘Student.name‘=>‘required|min:2|max:20‘, ‘Student.age‘=>‘required|integer‘, ‘Student.sex‘=>‘required|integer‘, ],[ ‘required‘=>‘:attribute 为必填项目‘, ‘min‘=>‘:attribute 长度必须符合要求‘, ],[ ‘Student.name‘=>‘姓名‘, ‘Student.age‘=>‘年龄‘, ‘Student.sex‘=>‘性别‘, ]); if ($val->fails()) { return redirect()->back()->withErrors($val)->withInput(); } 获取表单信息进行保存 $item= $request->input(‘Student‘); $isok= new Pet(); $isok->name=$item[‘name‘]; $isok->age=$item[‘age‘]; $isok->sex=$item[‘sex‘]; if( $isok->save()){ return redirect(‘pet/index‘)->with(‘session‘,‘添加成功!‘); }else{ return redirect()->back(); }
时间: 2024-10-14 20:26:52