首先创建Request:项目中我是在app->http->Request->自己创立一个文件夹(我的是User)->验证文件(CreateUserValidate) 这个文件夹建立的验证
CreateUserValidate代码块
1 <?php 2 3 namespace App\Http\Requests\User; 4 5 use App\Http\Requests\BaseValidate; 6 7 class CreateUserValidate extends BaseValidate 8 { 9 /** 10 * Determine if the user is authorized to make this request. 11 * 12 * @return bool 13 */ 14 public function authorize() 15 { 16 return true; 17 } 18 19 /** 20 * Get the validation rules that apply to the request. 21 * 22 * @return array 23 */ 24 public function rules() 25 { 26 if($this->isMethod('post')){ 27 return [ 28 'score' => 'required|min:6|max:6', 29 'userName' => 'required|between:2,4', 30 'userAge' => 'required|integer', 31 'addr' => 'required', 32 ]; 33 } 34 return []; 35 } 36 37 public function messages() 38 { 39 return [ 40 'score.required' => '分数必填', 41 'score.min' => '分数必需大于5位', 42 'score.max' => '分数必需小于7位', 43 'userName.required' => '用户名必填', 44 'userName.between' => '用户名长度必须在2:4之间', 45 'userAge.required' => '年龄必填', 46 'userAge.integer' => '年龄必需是整数', 47 'addr.required'=>'地址必填' 48 ]; 49 } 50 } 51
控制器具体方法
public function add(CreateUserValidate$request,$id){
}