Reputation: 6099
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
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