dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

Issue #190: Unique rule should maintain NULL as string when the attribute is NULL. #191

Closed danydev closed 6 years ago

danydev commented 6 years ago

After the functionality introduced in this PR https://github.com/dwightwatson/validating/pull/180 the semantic of the additional params of the 'NULL' string changed in a way that is not BC. This leads to produced queries that are not equivalent.

If I have a rule like unique:users,user_id,1,id,deleted,null what happens in prepareUniqueRule is that in $parameters the string null is replaced by a NULL value (returned by the model) and when the rule is generated by imploding $parameters, the NULL value is converted to an empty string, changing (and breaking) the semantic of the query.

The query produced by the broken rules goes from

SELECT COUNT(*) ... WHERE deleted IS NULL

to

SELECT COUNT(*) ... WHERE deleted = ""

This fix makes sure that NULL values returned by models are maintained as 'NULL' (as string), so that the resulting query matches our expectation.

dwightwatson commented 6 years ago

Good spot, thanks for this and apologies for breaking it the first place. Appreciate the test case so hopefully this won't happen again. I'll get a new tagged release out today.

danydev commented 6 years ago

Thank you! keep up with the great work!