'email' => 'required|email|unique:users should do it in terms of Validation
Update user e-mail to be unique (using Request class)
I have an account page where user can update his e-mail address. Now what I`m trying to do is to update it with checking if it is unique in users table, but with ignoring current user ID.
I red the documentation, but it doesn`t describe that situation (while using request class).
Here are my files:
UserController.php
public function update(UserUpdateRequest $request) {
$user = Auth::user();
$user->update($request->toArray());
Session::flash('message', 'The informations were successfully updated.');
return Redirect::back();
}
UserUpdateRequest.php
public function rules()
{
return [
'email' => 'required|unique:users,email,'.$id,
//other attributes...
];
}
This gives me error:
Undefined variable: id
After changing validation rule to that:
'email' => 'required|unique:users,email,'.$this->id,
while trying to update user and changing any other "attributes", but no "email" I got message that this e-mail is already taken so I think that it doesn`t see ID in validation rule. How to pass that id to rule?
So the parameter in the URL (and you can confirm this with php artisan route:list) for a resource route is the singular form of the resource
so in this case the url for edit would be users/{user}/edit
so what does $request->user contain?
Please or to participate in this conversation.