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

laravel unique email validation on update
laravel unique validation
laravel validation rules unique update
how to check unique email id in laravel
laravel validation unique except
laravel form request validation unique update
laravel form validation before submit
laravel validation unique column

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?

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

Unique email validation on Update (Laravel 5.6), A user has many posts, & there are many types of posts. All types of posts are getting validated by form requests php artisan make:request validat As seen above, setting unique:users as a validation rule will use the default database connection to query the database. To override this, specify the connection and the table name using "dot" syntax: 'email' => 'unique:connection.users,email_address' Forcing A Unique Rule To Ignore A Given ID:

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.

How to validate email uniqueness in my database, Hello, I'd like to start by saying that I am totally new buy in Laravel. public function update( Request $request, User $user ) { $this->validate( $request, [ '​name'  Unique email validation on Update (Laravel 5.6) A user has many posts, & there are many types of posts. All types of posts are getting validated by form requests php artisan make:request validateBusinessPost. But while updating the post, the email id is not passing the validation The email ID already exists.

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.

Validation - Laravel, Consider upgrading your project to Laravel 7.x. under validation. The second argument is the validation rules that should be applied to the data. 'email' => '​unique:users,email_address,NULL,id,account_id,1' Laravel attempts to take the pain out of development by easing common tasks used in most web projects. Append the id of the instance currently being updated to the validator. Pass the id of your instance to ignore the unique validator. In the validator, use a parameter to detect if you are updating or creating the resource. If updating, force the unique rule to ignore a given id:

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),
    ],
]);

Using the unique validation rule in a Laravel Form Request, Using the unique validation rule in Laravel to ensure that a field is unique in the If you have a form that the user has to fill in to create the post then you can either validate directly in the controller method for store and update, or you the validation into a form request class as this leaves your controller a bit  Laravel provides a couple of handy tools for this, including the Unique validation rule, but there is a bit more configuration required to other validation rules to make the most of its abilities. Let’s start with how the field is defined in the database before moving on to populating it.

Creat form request and add this code on App/Http/Request/YourFormRequest class

public function rules()
{  // get user by uri segment
   $user = User::find((int) request()->segment(2)); 
   return [ 
        'name' => 'required|string|max:100', 
        'email' => 'required|email|unique:users,email,'.$user->id.',id' 
    ];
}

check the doc here

Updating another user and ignoring its name/email when validating , In short, I have a user update form to update the name and email of a given For example: https://laravel.com/docs/5.4/validation#rule-unique  Laravel check for unique rule without itself in update me to exclude current user's email from validation: unique email out of the user that is updating it in

Laravel update model with unique validation rule for attribute , I have a Laravel User model whre I added unique validation rule on username and email . Now when I update my User through my Repo, and I  In Laravel all the emails sent by your application must be defined by a Mailable class. To generate one such class for our verify user’s email on registration. Let’s generate a class with artisan command. Run the given below command in your project root.

How to validate unique field value · Issue #187 · rebing/graphql-laravel, For example, i need to update user profile and email field should be unique. That's not a problem while creating an user: // https://laravel.com/  The Email Verification Column. Next, your user table must contain an email_verified_at column to store the date and time that the email address was verified. By default, the users table migration included with the Laravel framework already includes this column. So, all you need to do is run your database migrations:

Validation, To learn about Laravel's powerful validation features, let's look at a complete For more information on working with this object, check out its documentation. if the authenticated user actually has the authority to update a given resource. For example, to validate that each e-mail in a given array input field is unique, you  Hello, as an Administrator I want to be able to update User's Details (name, mail, dni, etc). I have a Request Controller called UserStoreRequest which validates that the email & dni are unique (this is also clarified in the migrations btw). The admin can search a User by his DNI and then a form is fullfiled with the user's data.

Comments
  • Only validate the email field if it's different? Something like... if ($user->email != $request->email). Clearly there should be some differences in checks between adding a user and updating a user.
  • No, I want to validate if it is unique. But I want to allow the user update his email to his CURRENT email without fail in the validation unique.
  • Just an FYI, this worked great in the 5.6.33 version. 'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,
  • 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.
  • 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.