#Trying to update email with unique user email.
30 messages · Page 1 of 1 (latest)
Please share actual code, not screenshots, makes copy/pasting your code harder when trying to help.
public function UpdateEmail(Request $request, User $user)
{
$validator=Validator::make($request->all(),[
'oldEmail' =>'required',
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($user)],
]);
if ($validator->fails()) {
return response()->json([
'message'=>'validations fails',
'errors' =>$validator->errors()
], 422);
}
$user=$request->user();
$user->update([
'email' => $request->email
]);
return response()->json([
'message'=>' email successfully updated',
'errors' =>$validator->errors()
], 200);
}
$validator = Validator::make($request->all(),[
'oldEmail' =>'required',
'email' => ['required', 'email', Rule::unique('users')->ignore($user)],
]);
You need to pass the user's ID to the ignore
Also, do not need to specify email column when the field being validated is the same name as the column.
Ignoring a model should work, right? I kinda feel like the user is actually an empty model (if there's no route model binding for that route?)
Actually you are correct Robert, just checked the core method.
still receiving
Request failed with status code 422
does it matter that my id is called _id on the users table?
Do you specify that as your PK on the user model? Assume you do if you are getting a properly route bound user model per your controller method.
yes i do
So, what are you receiving for validation errors? Hard to tell what's going on without knowing what the actual error is
Can you show what you are posting as well?
i am calling this from an react native app so its just showing me a 422 error code
Is this an admin endpoint, where you update the specified user from the bound model ID
const handleReset = async () => {
try {
const result = await axios.put(/api/updateEmail, {
oldPassword: oldEmail,
email: email,
});
resetTextInput();
setUpdateMessage(true);
return result.data;
} catch (e) {
setCodeErrorMessage(true);
console.log(e);
}
};
So your user model is empty
I'm sure it has debugging capabilities tho, otherwise debugging would be a nightmare
Your controller specifies a user, but you are not sending a user ID to your API call, thus your user model is an empty instance.
and password ye
Im sorry guys for wasting your time
No worries, glad you got it sorted.
Is this just for a user to change their email? Usually I would require their current password + new email to change it.
Don't worry, rubber duch debugging can be extremely helpful 😄 and it keeps @brazen narwhal's brain fresh
thats a very good point.