Laravel Validation: soft delete + unique email + update

Viewed 1030

I'm trying to set up a client registration check and profile edit.

  1. I'm using soft delete
  2. E-mail has to be unique (not counting the delted ones of course)
  3. If profile is edited (id is present) it has to ignore that fields email

So far I have

'email' => [
    'required',
    'email',
    'unique:clients,email,NULL,deleted_at,deleted_at,NULL',
],

Now this works correctly and detects the unique emails that are not deleted, but it gives me errors when I need to edit the profile off the client. I tried adding additional unique settings, but they seem to be getting ignored:

'email' => [
    'required',
    'email',
    'unique:clients,email,NULL,deleted_at,deleted_at,NULL',
    Rule::unique('clients')->ignore('id', (int)$request->input('id')) // <- from Laravel docs, but is not working
],

I found solutions with only one of these requirements, but I can't seem to combine them.

Edit: The fix was

'email'=>['required','email',Rule::unique('clients','email')->ignore($request->input('id'))->whereNull('deleted_at')],
1 Answers

try this

 $this->validate($request,[
            'email'=>['required','email',Rule::unique('clients','email')->ignore($id)->whereNull('deleted_at')]
        ]); 

Related