如何验证在 Laravel 中更新电子邮件的用户的唯一电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40942367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How validate unique email out of the user that is updating it in Laravel?
提问by Luiz
I am using Laravel 5.2 and want to update user's account using validator.
我正在使用 Laravel 5.2 并想使用验证器更新用户的帐户。
I want to keep email field unique, but, if the user type his current email it will break. How can I update if the email is unique, except the user's own current email?
我想保持电子邮件字段的唯一性,但是,如果用户输入他当前的电子邮件,它将中断。除了用户自己的当前电子邮件之外,如果电子邮件是唯一的,我该如何更新?
回答by Antonio Carlos Ribeiro
回答by Afraz Ahmad
In Request Class you will probably need this validation in PUT or PATCH method where you don't have user then you can simply use this rule
在请求类中,您可能需要在没有用户的 PUT 或 PATCH 方法中进行此验证,然后您可以简单地使用此规则
You have 2 options to do this
1:
1:
'email' => "unique:users,email,$this->id,id"
OR
或者
2:
2:
use Illuminate\Validation\Rule; //import Rule class
'email' => Rule::unique('users')->ignore($this->id); //use it in PUT or PATCH method
$this->idis providing id of the user because $thisis object of Request Class and Request also contains user object.
$this->id提供用户的 id,因为$this是请求类的对象,请求也包含用户对象。
public function rules()
{
switch ($this->method()) {
case 'POST':
{
return [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required'
];
}
case 'PUT':
case 'PATCH':
{
return [
'name' => 'required',
'email' => "unique:users,email,$this->id,id",
OR
//below way will only work in Laravel ^5.5
'email' => Rule::unique('users')->ignore($this->id),
//Sometimes you dont have id in $this object
//then you can use route method to get object of model
//and then get the id or slug whatever you want like below:
'email' => Rule::unique('users')->ignore($this->route()->user->id),
];
}
default: break;
}
}
Hope it will solve the problem while using request class.
希望它会在使用请求类时解决问题。
回答by Salam
For coders using FormRequest
& Laravel 5.7 and facing this problem, you can do something like this
对于使用FormRequest
& Laravel 5.7 并面临此问题的编码人员,您可以执行以下操作
public function rules() {
return [
'email' => ['required', 'string', 'email', 'max:255',
Rule::unique('users')->ignore($this->user),
],
];
}
The $this->user
will return the user ID coming from the request.
该$this->user
会返回从请求传来的用户ID。
回答by Juan Carlos Ibarra
On Laravel 5.7 to instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
在 Laravel 5.7 上指示验证器忽略用户 ID,我们将使用 Rule 类来流畅地定义规则。在这个例子中,我们还将验证规则指定为一个数组,而不是使用 | 分隔规则的字符:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
回答by silvia zulinka
Creat form request and add this code on App/Http/Request/YourFormRequest class
创建表单请求并在 App/Http/Request/YourFormRequest 类上添加此代码
public function rules()
{ // get user by uri segment
$user = User::find((int) request()->segment(2));
return [
'name' => 'required|string|max:100',
'email' => 'required|email|unique:users,email,'.$user->id.',id'
];
}
check the doc here
在此处查看文档
回答by Vincent Decaux
On Laravel 7 to build an API, if you want something clean, you can simply use :
在 Laravel 7 上构建 API,如果你想要一些干净的东西,你可以简单地使用:
public function rules()
{
$emailRule = Rule::unique((new User)->getTable());
if (request()->isMethod('put')) {
// we update user, let's ignore its own email
// consider your route like : PUT /users/{user}
$emailRule->ignore($this->route('user'));
}
return [
'name' => 'required',
'email' => [
'required',
'email',
$emailRule
],
];
}
You can get the user you want to update (using PUT
method here) and ignore him.
您可以获取要更新的用户(使用PUT
此处的方法)并忽略他。