The email has already taken in edit/update state I don't understand this rule gives me email has already taken when I'm editing the user info.
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore($user->id)],
edit.vue
const form = useForm({
//
email: props.user.email,
});
It's telling you that the email is already in the database. What are you trying to do.
@jlrdw I'm trying to make user profile data editing. When editing the user data I don't want to change the email field. You know that.
@dev -chahal
UpdateUserRequest.php
public function rules(User $user)
{
return [
'name' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore($user->id)],
];
}
@shaungbhone Does this rule gives you email has already taken when you don't change email field but other fields ?
@shaungbhone injecting the user will not give you one from the database. So id is null. Is it the auth user?
public function rules()
{
return [
'name' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore(auth()->user()->id)],
];
}
@shaungbhone then you need to get that is. If you dd() your user I'm sure id is missing
Try this instead
public function rules()
{
return [
'name' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore($this->segment(2))],
];
}
@Sinnbeck No no. I mean this id is dynamic. What about 3, 4, 5 etc? admin can update many user profiles. I think my rule in UpdateRequest is correct. Where am I wrong?
@shaungbhone ok we can take it step by step
First, what does this give you?
public function rules(User $user)
{
dd($user->id);
return [
'name' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore($user->id)],
];
}
@shaungbhone exactly. Check above where I mentioned this
and this?
public function rules()
{
dd($this->segment(2));
return [
'name' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', Rule::unique('users')->ignore($this->segment(2))],
];
}
@shaungbhone exactly. The id you are trying to get.
You can also try
dd($this->route('user'));
@Sinnbeck Thank you. Can you explain $user->id is give me null?
Fixed typo in my last answer
@shaungbhone because rules() isn't a controller method so it isn't getting the route model bindings
Please sign in or create an account to participate in this conversation.