How validate unique email out of the user that is updating it in Laravel?

103,444

Solution 1

You can tell that to validators:

'email' => 'unique:users,email_address,'.$user->id

Check the docs, in section 'Forcing A Unique Rule To Ignore A Given ID'.

Solution 2

In Request Class you will probably need this validation in PUT or PATCH method where you don't have user then you can simply use this rule

 You have 2 options to do this

1:

 'email' => "unique:users,email,$this->id,id"

OR

2:

 use Illuminate\Validation\Rule; //import Rule class 
'email' => Rule::unique('users')->ignore($this->id); //use it in PUT or PATCH method

$this->id is providing id of the user because $this is object of Request Class and Request also contains user object.

public function rules()
{
    switch ($this->method()) {
        case 'POST':
        {
            return [
                'name' => 'required',
                'email' => 'required|email|unique:users',
                'password' => 'required'
            ];
        }
        case 'PUT':
        case 'PATCH':
        {
            return [
                'name' => 'required',
                'email' => "unique:users,email,$this->id,id",
                                   OR
                //below way will only work in Laravel ^5.5 
                'email' => Rule::unique('users')->ignore($this->id),

               //Sometimes you dont have id in $this object
               //then you can use route method to get object of model 
               //and then get the id or slug whatever you want like below:

              'email' => Rule::unique('users')->ignore($this->route()->user->id),
            ];
        }
        default: break;
    }
}

Hope it will solve the problem while using request class.

Solution 3

On Laravel 5.7+ to instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:

use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => ['required',Rule::unique('users')->ignore($user->id)],
]);

Solution 4

For coders using FormRequest & Laravel 5.7 and facing this problem, you can do something like this

public function rules() {

    return [
        'email' => ['required', 'string', 'email', 'max:255',
            Rule::unique('users')->ignore($this->user),
        ],
    ];

}

The $this->user will return the user ID coming from the request.

Solution 5

'email' => "required|email|unique:users,email,$id",

In a laravel 8. I also search for long time. this will work

Share:
103,444
Luiz
Author by

Luiz

I am a passionate Computer Scientist. My main interests are web development, with emphasis on search engines and machine learning, and Internet of Things, with emphasis on agricultural automation. I also love to work with Operating Systems architectures and their subareas.

Updated on July 09, 2022

Comments

  • Luiz
    Luiz almost 2 years

    I am using Laravel 5.2 and want to update user's account using validator.

    I want to keep email field unique, but, if the user type his current email it will break. How can I update if the email is unique, except the user's own current email?

  • daugaard47
    daugaard47 over 5 years
    Just an FYI, this worked great in the 5.6.33 version. 'email' => 'required|string|email|max:255|unique:users,email,'.$user->i‌​d,
  • Robin Drost
    Robin Drost almost 5 years
    This will only work for the current logged in user. If the user who is editing the user is for example an admin this solution will not work. You however can get the user ID from the route (if available) $this->route('id') or $this->route('user') when using resource routes.
  • Juan Carlos Ibarra
    Juan Carlos Ibarra almost 5 years
    Exactly the $user variable is a representation of the user email you want to ignore, it can be the auth()->user() or a request('user_id') depending on your project.
  • Sakibul Alam
    Sakibul Alam almost 3 years
    it will be useful for others to quickly grasp your solution if you add a bit of formatting to the code and also describe what you have done or add references.
  • Salman Shahid
    Salman Shahid over 2 years
    Works like a charm