[5.6] Ignore a given model during a unique check.#23524
Conversation
| $this->column, | ||
| $this->ignore ? '"'.$this->ignore.'"' : 'NULL', | ||
| $this->idColumn, | ||
| $this->idColumn ?? 'NULL', |
There was a problem hiding this comment.
Can you explain why this is needed?
There was a problem hiding this comment.
because we removed the default value for the protected $idColumn; and the __toString() need string conversion for the variables
| @@ -0,0 +1 @@ | |||
| hi No newline at end of file | |||
There was a problem hiding this comment.
I don't know, it was wrongly added. Sorry for that.
| $rule = new \Illuminate\Validation\Rules\Unique('table'); | ||
| $rule->where('foo', 'bar'); | ||
| $this->assertEquals('unique:table,NULL,NULL,id,foo,bar', (string) $rule); | ||
| $this->assertEquals('unique:table,NULL,NULL,NULL,foo,bar', (string) $rule); |
There was a problem hiding this comment.
It makes me uncomfortable having to change an old test to add this feature.
There was a problem hiding this comment.
I think we don't need the default value for the idColumn that's why i had to change the test here, because we really don't need the 'id' since the ignore is NULL. but I can revert this change and still the feature will pass.
There was a problem hiding this comment.
OK please don't change that test.
There was a problem hiding this comment.
Done, Sorry for that 👍
This PR adds the ability to ignore a given model during a unique validation rule check instead of passing the column's value and name to the ignore() method, we can pass the actual model to the ignore and by default the rule will grab the primary key name and value for the model. Also it adds the ability to select a custom column and the corresponding value will be retrieved from the model.
Let's Consider we have a Company model that we need to update it's name and the company names should be unique, therefore the validation rule will be as follows