Eloquent select rows with empty string or null value

Eloquent select rows with empty string or null value

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling NULL and Empty Strings in Eloquent Queries

When constructing database queries, distinguishing between a value that doesn't exist (NULL) and a value that explicitly exists but is empty ('') often trips up developers. In standard SQL, these are distinct states. However, when mapping this logic to Laravel's Eloquent ORM, the syntax can be counterintuitive because the framework attempts to optimize queries by merging conditions rather than treating them as separate logical branches in the way a raw WHERE clause might suggest.

Consider a scenario where you have a model relationship that filters for records where a specific column is NULL. A common pattern looks like this:

$user->albums()->where('col', NULL);

This query typically translates into SQL using IS NULL, which correctly targets rows where the column contains no value. The issue arises when you attempt to extend this logic to include empty strings using the chained or_where method:

$user->albums()->where('col', NULL)->orWhere('col', '');

While this code executes without throwing an exception, it often fails to return the expected results. Instead of generating a query that checks for either condition, Eloquent might be generating a query that effectively treats the empty string check as an IS comparison rather than a value match, or the query builder merges the conditions in a way that ignores the intent of the orWhere operator in this specific context.

The root cause lies in how Eloquent's grammar handles the conversion of NULL and empty strings into SQL tokens. When you pass NULL directly to the where method, Eloquent uses a special grammar rule to generate IS NULL. However, when you follow it with orWhere('col', ''), the framework sees a string value. In many database dialects, comparing a column to an empty string using the standard equality operator (=) behaves differently depending on whether the column is nullable. If the column allows NULL, column = '' will not match rows where column IS NULL.

To reliably select rows that contain either a NULL value or an empty string, you must explicitly instruct Eloquent to treat both conditions as distinct logical checks within the same query scope. The most robust approach is to use the whereNull helper for the null check and combine it with a standard orWhere for the empty string, ensuring the conditions are properly separated by the logical operator.

$user->albums()
    ->whereNull('col')
    ->orWhere('col', '')
    ->get();

This pattern forces Eloquent to generate SQL that looks something like this:

SELECT * FROM albums
WHERE col IS NULL OR col = ''

The whereNull method is the dedicated syntax for handling null checks because it bypasses the standard equality logic. While you might have seen discussions suggesting that whereNull works differently than documented in older versions, relying on explicit separation of concerns ensures compatibility across different database drivers like MySQL, PostgreSQL, and SQLite.

It is also worth noting that if your application relies heavily on handling "falsy" values (nulls and empty strings) for a specific column, you might consider normalizing your data to prevent NULL and '' from coexisting unless there is a strict business reason for doing so. However, if the database schema dictates that both states are valid and meaningful, using the whereNull combined with orWhere pattern remains the standard solution within the Laravel ecosystem.

For further reading on advanced query scopes and how to handle complex filtering logic in Eloquent, you can refer to https://laravelcompany.com.

Our network