V4n1ll4
V4n1ll4

Reputation: 6099

Laravel 5.1 unique email

I'm trying to use the following rule in my code as per the documentation, however it is not working:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'first_name'        => 'required',
        'last_name'         => 'required',
        'email'             => 'required|unique:users,email,:email',
    ];
}

It comes back with The email has already been taken.

I am trying to say if the users email is not unique then throw an error, except for the current user id.

How can I achieve this?

Upvotes: 1

Views: 1241

Answers (1)

Stuart Wagner
Stuart Wagner

Reputation: 2067

You need to give the unique rule an ID to ignore. Try this:

return [
    'first_name'        => 'required',
    'last_name'         => 'required',
    'email'             => 'required|unique:users,email,' . Auth::user()->id
];

Note: This assumes you are using Laravel's Auth class.

Upvotes: 3

Related Questions