I want to validate that the name field in the database table pools is unique. When I'm creating a new entry, this works fine:
$rules = [
'name' => 'required|unique:pools'
];
However, when I want to update an existing record, then the uniqueness check should skip the item being updated (as, obviously, the name could be unchanged). So I dug around and found this option:
$rules = [
'name' => 'required|unique:pools,id,1'
];
Where 1 is the ID of the record I'm updating. This seemed to resolve the issue, but then I tested with a different ID (in which case the validator should return an error) but it still allows a new record with the same name to be added.
For example, if I do
$rules = [
'name' => 'required|unique:pools,id,4'
];
Where 4 is an ID of a completely different entry in the DB (or even an ID that doesn't exist at all), the uniqueness check doesn't work and allows me to make as many duplicate records as I want. It would seem that adding any ID to the above rule makes the validator ignore all matches in the DB, except just the one with the given ID.
Basically, I have a similar problem to the one outlined in this question, except the answer doesn't work for me:
Laravel 4 Validation unique(database) ignore current
Anyone with a solution?