添加成功的例子过了,但是添加错误的时候而比如不输入年龄就直接会报错,而不是返回页面,
<!-- 成功提示框 --> @if(Session::has('success')) <div class="alert alert-success alert-dismissible"> <button type="buttn" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button> <strong>成功!</strong>{{session::get('success')}}<!-- 操作成功提示 --> </div> @endif <!-- 失败提示框 --> @if(Session::has('error')) <div class="alert alert-danger alert-dismissible"> <button type="buttn" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button> <strong>失败!</strong><!-- 操作失败提示 --> {{session::get('error')}} </div> @endif
public function save(Request $request){ $data=$request->input('student'); $student=new Studentm(); $student->name=$data['name']; $student->age=$data['age']; $student->sex=$data['sex']; if(!$student->save()){ return redirect('Student/index')->with('success','添加成功'); }else{ redirect()->back(); } return view('student.create'); }
没有添加验证 需要在添加之前加入验证
$validator=\Validator::make($request->input(),[ 'student.name'=>'required|min:2|max:10', 'student.age'=>'required|integer', 'student.sex'=>'required|integer' ],[ 'required'=>":attribute不能为空", 'integer'=>":attribute必须是整数", 'min'=>':attribute最小不能少于2字符' ],[ 'student.name'=>'姓名', 'student.age'=>'年龄', 'student.sex'=>'性别' ]); if($validator->fails()){ return redirect()->back()->withErrors($validator)->withInput(); }