logo

A cross-database method for null-safe equality

Laravel quietly added it earlier this year

Joel Clermont
Joel Clermont
2026-05-21

In a recent tip, I shared how comparisons with NULL values might not work the way you expect.

My recommendation was to handle those rows explicitly with an extra whereNull clause.

MySQL also has a <=> operator built for null-safe equality, and my first instinct was that I would need whereRaw to reach for it. Aaron questioned whether the raw clause was actually necessary, and that sent me digging.

It turns out Laravel added a whereNullSafeEquals method that compares a column to a value while treating two NULL values as equal. It landed in a February 2026 pull request and isn't in the documentation yet, so it's easy to miss.

$query->whereNullSafeEquals('last_fired_on', $date);

The interesting part is what it compiles to. Every database engine handles null-safe equality differently, and this one method papers over all of them.

-- MySQL
where `last_fired_on` <=> ?

-- PostgreSQL
where "last_fired_on" is not distinct from ?

-- SQL Server
where exists (select [last_fired_on] intersect select ?)

There's a nice bonus. MySQL's <=> operator is recognized as a first-class operator in a normal where call, and Laravel routes it through that same cross-database machinery.

$query->where('last_fired_on', '<=>', $date);

So the MySQL-looking operator isn't locking you to MySQL. It produces the exact same portable SQL the method does.

And for the not-equal case from the original tip, whereNot composes with it cleanly.

$query->whereNot('last_fired_on', '<=>', $date);

Here to help,

Joel

P.S. The subtle stuff is exactly what a fresh set of eyes catches in a Laravel code review.

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.