If you have a unique value in your model, and you perform model update, it can happen you will get the message "The value has already been taken". Let's show this on next example.
You could have a user table and field email which contains unique value, or in our example we will use promo codes table where we have unique value for code.
So the validation is set in model itself and looks like this:
protected $rules = [ 'code' => 'required|string|max:32|unique:promocodes', 'description' => 'required|alpha_num', //... ];
And now when we try to update this promo code and change description we will get the error message:
"The code has already been taken"
But, we do not want to change the 'code' value, we are just changing description...? WTF?
DB in this case says: you are trying to update the unique code value to a value which already exist... which is kind of true... but.... how to fix this? I need to be able to update other fields...
Well the solution for updating table (model) in Laravel which has unique value is dead simple:
in your validation string you just have to add :id behind unique value params.
So, our validation will look like this.
protected $rules = [ 'code' => 'required|string|max:32|unique:promocodes,id', // => added id 'description' => 'required|alpha_num', //... ];
What this id does? Well it basically says, ignore this id of the promocode when performing checks for uniqueness.
More about this you can read in official Laravel documentation