Search code examples
phplaravelvalidationlaravel-5request

Laravel request update unique field only if changed


I want to be able to validate a user email address based on certain circumstances.

For example

If a user is being created, the email address must be unique

If a user is being updated but the email address hasn't changed, ignore the unique email rule

If a user is being updated and their email address has changed, it has to be unique

I've had a little look around and I know that I can specify different rules based on the method like so

public function rules()
{
    $user = User::find($this->users);

    switch($this->method())
    {
        case 'POST':
        {
            return [
                'user.email'      => 'required|email|unique:users,email',
            ];
        }
        case 'PUT':
        case 'PATCH':
        {
            return [
                'user.email'      => 'required|email,
            ];
        }
        default:break;
    }
}

Is there any way to make it so that in the put/patch case, the rule checks if the email address has been changed, and if it has then it has to be unique?

If not is there a cleaner way of achieving this goal? Maybe without the switch case? Perhaps more in depth validation rules I haven't stumbled across yet?


Solution

  • If i understand you correctly, you want to add unique validation if email changed, if that is the case then you just need to add a condition in validation. If you check unique validation structure it look like this unique:table,column,except,idColumn, check documentation

    So the validation will be look like this, when you will create new record $userId will be null but at time of patch it will have value. So this validation will work for both create and patch.

    $userId = isset($user) ? $user->id : null;
    
    $rules = [
            'email' => 'required|email|unique:users,email,'. $userId.',id',
        ];
    
     $this->validate($request, $rules);