我知道这个问题以前被问过很多次,但是没有人解释在模型中验证时如何获得 id。
'email' => 'unique:users,email_address,10'
验证规则在模型中,因此如何将记录的 ID 传递给验证规则。
这是我的模型/用户
protected $rules_update = [
'email_address' => 'required|email|unique:users,email_address,'.$id,
'first_name' => "required",
'last_name' => "required",
'password' => "required|min:6|same:password_confirm",
'password_confirm' => "required:min:6|same:password",
'password_current' => "required:min:6"
];
模型/基本模型
protected $rules = array();
public $errors;
/*
* @data: array, Data to be validated
* @rules: string, rule name in model
*/
public function validate($data, $rules = "rules") {
$validation = Validator::make($data, $this->$rules);
if($validation->passes()) {
return true;
}
$this->errors = $validation->messages();
return false;
}