logo

Rule::exists vs Rule::unique

Maybe you don't need that custom rule

Joel Clermont
Joel Clermont
2026-01-27

I was reviewing code on a new project, and I saw very "interesting" use of a custom validation rule. Internally, it was using the built-in Rule::exists method, but then it was inverting it and returning false if that particular rule found any matching records.

Laravel already has a built-in rule that does this: Rule::unique.

To prove that I wasn't missing anything, I first wrote some tests for the controller action that was relying on this custom rule, to make sure I fully understood how it was working.

Then, with all the tests passing, I removed that custom rule and replaced it was a direct call to Rule::unique and re-ran the tests to make sure nothing broke.

There are valid times to need to write a custom rule, but this is a reminder to occasionally check the validation docs.

Things are being added all the time and that custom rule you're about to write might already be built for you in the framework.

Here to help,

Joel

P.S. Would you like us to review your code and look for opportunities to lean more heavily on built-in Laravel functionality?

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice.