How do you check "if not null" with Eloquent?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Checking If a Field is Not Null with Eloquent - A Comprehensive Guide
Introduction:
Eloquent, the object-relational mapping (ORM) library in Laravel, provides developers with an easy and streamlined way to interact with databases. However, sometimes we need to check specific conditions on our database entries, such as whether a field is not null or empty. Unfortunately, the syntax for these checks isn't always intuitive. In this blog post, we will explore several methods for checking if a field is not null in Eloquent queries.
Method 1: Using the Null Coalescing Operator (?:)
The first method involves using Laravel's ternary operator (?:) to check whether the field is not null before performing any further comparisons or actions. Here's an example of how we can use this approach with a boolean field named 'is_verified':
$users = User::where('is_verified', function ($query) {
$query->whereNotNull('email')
->where(function($q) {
$q->havingRaw('NOT (exists (select * from users where email is null))');
});
})->get();
In this example, we start by checking if the 'is_verified' field has a value. If it does not have a value (null), then we are using the null coalescing operator to assign the result of the following query to the 'email' column as its value:
'NOT (exists (select * from users where email is null))';
This ensures that the only users returned will be those who have a verified status and also have a non-null 'email' field. The query can be simplified by using the has() method as follows:
$users = User::whereHas('emails', function ($query) {
$query->whereNotNull('email');
})->where('is_verified', true)->get();
This ensures that only users with a verified status who have a non-null 'email' field will be retrieved. This method allows us to perform multiple checks and is flexible, but it can become verbose in more complex scenarios. For a less verbose alternative, we can use the 'whereNotNull()' clause.
Method 2: Using WhereNotNull() Clause
The WhereNotNull() clause can also be used to check if a field is not null. Here's how you can use it for the same scenario as above:
$users = User::where(function ($query) {
$query->where('is_verified', true);
})->whereHas('emails', function ($query) {
$query->whereNotNull('email');
})->get();
In this example, we start by checking if the 'is_verified' field is set. Next, we check for users with valid email addresses. This approach provides a clear and easy-to-read solution to our problem. However, it can be limited when dealing with nested queries or more complex conditions. For such cases, we can use the DB facade to our advantage.
Method 3: Using the DB Facade
The Laravel Database (DB) facade allows us to build and execute raw SQL queries using Eloquent models. This is particularly useful when dealing with complex or nested queries. Here's how you can achieve the same result by combining the 'whereNotNull()' clause with a custom query:
$users = User::where(function ($query) {
$query->orWhereRaw('is_verified IS NOT NULL');
})->whereHas('emails', function ($query) {
$query->whereNotNull('email');
})->get();
In this example, we add an 'OR' condition to our query for users with a non-null 'is_verified' field and continue building the query. We then apply the same filter to find users with valid email addresses as before. This method provides us with great flexibility when dealing with complex queries but requires more experience with raw SQL syntax.
Conclusion:
In conclusion, there are multiple ways to check if a field is not null in Eloquent. You can use a combination of Laravel's ternary operator (?:), the 'whereNotNull()' clause, and the DB facade to find the best solution for your specific query. Remember that it's always wise to choose the simplest method possible as complex queries might be more difficult to maintain in the long run. By following these guidelines, you will ensure that your Laravel application can efficiently handle database interactions and handle null fields accordingly.