Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaungbhone's avatar

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,
});
0 likes
20 replies
jlrdw's avatar

It's telling you that the email is already in the database. What are you trying to do.

shaungbhone's avatar

@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.

shaungbhone's avatar

@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)],
  ];
}
chahal's avatar

@shaungbhone Does this rule gives you email has already taken when you don't change email field but other fields ?

Sinnbeck's avatar

@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)],
  ];
} 
Sinnbeck's avatar

@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))],
  ];
} 
shaungbhone's avatar

@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?

Sinnbeck's avatar

@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)],
  ];
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@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))],
  ];
}  
Sinnbeck's avatar

@shaungbhone because rules() isn't a controller method so it isn't getting the route model bindings

1 like

Please or to participate in this conversation.