我有一个用于编辑用户帐户的表单。我在电子邮件字段上有一些验证设置,我已指定电子邮件必须是唯一的。问题是当我提交表单时,验证规则要求电子邮件是唯一的火灾。那不应该是这种情况,因为我正在编辑表格,我知道电子邮件存在。
这是代码
public function servicesAdminEditUser(){
$id = request('hidden_user_id');
$rules = array(
'users_telephone_number' => 'required|unique:users,users_telephone_number',
'users_email' => 'required|email|unique:users,email',
'users_full_names' => 'required',
'users_gender' => 'required',
'users_credits' => 'required',
'users_email_verification_status' => 'required',
'users_number_verification_status' => 'required',
'users_moderation_status' => 'required',
'user_role' => 'required',
'users_profile_picture' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
);
$customMessages = [
'users_telephone_number.required' => 'The telephone number is required.',
'users_email.required' => 'We need a unique email',
'users_full_names.required' => 'Your full names are required',
'users_gender.required' => 'Gender is required',
'users_profile_picture.required' => 'We need your profile picture',
'users_credits.required' => 'User Credits Are Required',
'users_email_verification_status.required' => 'User email verification status is required',
'users_number_verification_status.required' => 'User number verification status is required',
'users_moderation_status.required' => 'User moderation status is required',
'user_role.required' => 'User role is required',
];
$validator = Validator::make(Input::all(), $rules,$customMessages);
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::back()->withErrors($validator);
}
else {
if (Input::file('users_profile_picture')->isValid()) {
$destinationPath = 'uploads';
$extension = Input::file('users_profile_picture')->getClientOriginalExtension();
$fileName = time().'_'.rand(11111,99999).'.'.$extension;
Input::file('users_profile_picture')->move($destinationPath, $fileName);
}
else {
echo 'file not valid';
}
$updyt = User::find($id);
$updyt->email = request('users_email');
//$reg->password = bcrypt(request('confirm-password'));
$updyt->users_telephone_number = request('users_telephone_number');
$updyt->users_email_verification_status = request('users_email_verification_status');
$updyt->users_number_verification_status = request('users_number_verification_status');
$updyt->users_full_names = request('users_full_names');
$updyt->users_gender = request('users_gender');
$updyt->users_credits = request('users_credits');
$updyt->users_profile_picture = $fileName;
$updyt->users_moderation_status = request('users_moderation_status');;
$updyt->update();
//$reg->attachRole($value);
DB::table('role_user')->where('user_id',$id)->delete();
$updyt->attachRole(Role::where('name',request('user_role'))->first());
return redirect('servicesUsers');
}
}
如果我删除了唯一规则,那么用户编辑某个帐户就会输入现有的电子邮件,laravel将显示在浏览器上捕获重复项的mysql错误。我该如何解决这个问题?。
答案 0 :(得分:2)
试试这个,
假设您的电子邮件字段名称为d1
,请求输入邮件字段名称为email
,而users_email
是$id
中用户的ID。
users table
答案 1 :(得分:0)
如果您想忽略某些ID(在这种情况下,对于更新),您可以使用以下语法:
唯一:表,列,除了,idColumn 强>
验证字段在给定的数据库表中必须是唯一的。如果未指定列选项,则将使用字段名称。
'users_email' => "required|email|unique:users,email,id,{$id}",
我建议您创建另一种更新方法,这将使您的代码更清晰,更易于理解。