The little problem is testing a user editing form, when I validate the data with a custom request, it causes me problems when I have unique fields.
In my case, I have the email as the only field, when I edit and I do not want to modify the email, it throws me the message that it is taken.
public function rules()
{
return [
'name' => 'required|string|min:3|max:60',
'email' => 'required|email|unique:users',
];
}
The first thing that occurred to me was to modify the Request rule where it says that it is unique, but this allows me to enter an email that is already in the database and say nothing, that's fine, in my database it is also restricted and he does not put it, but I would like that in that case if he warns him that it is taken
The next thing that occurred to me was to do this part in the controller, but I can't verify the existence of the email inside the DB, that is, if I do a where where I get a users if the email exists or an empty collection if not, but there it is .
In the Laravel documentation this case is explained:
https://laravel.com/docs/5.3/validation#rule-unique
In case you need something more complex, the idea is to use the class
Rule
and the ignore method:or depending on the case:
Remember that unique in the rules method has 3 parameters:
The first parameter defines the table, the second the column, the third is which row should be ignored (with its id)
In this case if you do a dump and die
at the start of the method, you will get all the parameters of the request. As you should know inside the request is the route you are trying to access (mysite.com/user/edit/{user} => where {user} is the id of the user you want to modify), therefore with $this ->route('user') you access the variable {user} written in the route.
Greetings.
I do it in the following way:
In the route method, the "users.id" parameter is the name of the route resource (in my case it's users) next to the id field of the table.
In addition, the method serves me both to insert and to update.
If it doesn't work, do a dd(this->route('users.id') or whatever the users route plus the id is called and check if it is actually returning the user's id.
Greetings.