gpt4 book ai didi

Laravel 配置文件更新,电子邮件唯一 :users

转载 作者:行者123 更新时间:2023-12-01 07:06:37 25 4
gpt4 key购买 nike

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

public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
];
}
我想限制该用户使用其他人正在使用的相同电子邮件......但如果这是该用户配置文件中已有的相同电子邮件并且他不想更改它,我想忽略它。
public function updateData(UpdateDataRequest $request)
{
DB::table('users')
->where('id', Auth::user()->id)
->update(array('email' => $request->email, 'name' => $request->name));

return redirect('panel');
}
怎么做才对?

最佳答案

这种确切的情况在文档中用作示例。
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'

关于Laravel 配置文件更新,电子邮件唯一 :users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34819086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com