Laravel 个人资料更新与电子邮件唯一:用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34819086/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:02:55  来源:igfitidea点击:

Laravel profile update with e-mail unique:users

laravel

提问by Krystus

I'm new in Laravel. I try to make profile update page... all works good but if I try to apply rule to set email field unique:users I have problem when user try to update for example name and don't want change email.

我是 Laravel 的新手。我尝试制作个人资料更新页面...一切正常,但如果我尝试应用规则来设置唯一的电子邮件字段:users 当用户尝试更新例如姓名并且不想更改电子邮件时,我会遇到问题。

public function rules()
    {
        return [
          'name' => 'required|max:255',
          'email' => 'required|email|max:255|unique:users',
        ];
    }

I want restrict that user to use the same e-mail that someone else is using... but I want to ignore that if this is the same e-mail already in that user profile and he don't want to change that.

我想限制该用户使用其他人正在使用的相同电子邮件......但如果这是该用户配置文件中已有的相同电子邮件并且他不想更改它,我想忽略它。

public function updateData(UpdateDataRequest $request){

公共函数 updateData(UpdateDataRequest $request){

DB::table('users')
    ->where('id', Auth::user()->id)
    ->update(array('email' => $request->email, 'name' => $request->name));

return redirect('panel');

}

}

How to do it right?

怎么做才对?

回答by ceejayoz

This exact situation is used as an example in the docs.

这种确切的情况在文档中用作示例。

https://laravel.com/docs/5.2/validation#rule-unique

https://laravel.com/docs/5.2/validation#rule-unique

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:

'email' => 'unique:users,email_address,'.$user->id

If your table uses a primary key column name other than id, you may specify it as the fourth parameter:

'email' => 'unique:users,email_address,'.$user->id.',user_id'

强制唯一规则忽略给定 ID:

有时,您可能希望在唯一性检查期间忽略给定的 ID。例如,考虑一个包含用户姓名、电子邮件地址和位置的“更新配置文件”屏幕。当然,您需要验证电子邮件地址是否唯一。但是,如果用户只更改姓名字段而不更改电子邮件字段,您不希望抛出验证错误,因为用户已经是电子邮件地址的所有者。如果用户提供的电子邮件地址已被其他用户使用,您只想抛出验证错误。要告诉唯一规则忽略用户的 ID,您可以将 ID 作为第三个参数传递:

'email' => 'unique:users,email_address,'.$user->id

如果您的表使用 id 以外的主键列名,您可以将其指定为第四个参数:

'email' => 'unique:users,email_address,'.$user->id.',user_id'

回答by AbdulAhmad Matin

In new version. laravelusing Rules to ignore any user or record

在新版本中。laravel使用规则忽略任何用户或记录

https://laravel.com/docs/5.8/validation#rule-unique

https://laravel.com/docs/5.8/validation#rule-unique

use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

$user->idcan be a specific id or the id of current user which is login

$user->id可以是特定的 id 或当前登录用户的 id