Laravel:更新时验证独一无二
2022-08-30 07:32:05
我知道这个问题以前已经问过很多次了,但是当你在模型中验证时,没有人解释如何获取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;
}