Laravel whereNull and whereNotNull Eloquent Query Example

Laravel whereNull and whereNotNull query example; In this tutorial we are going to share how to use laravel where null and where not null eloquent query in laravel app.

Laravel provides us more then laravel eloquent query builder same as sql queries, just like sql query where null and where not null laravel also provide where null or where not null eloquent query for adding on model.

Where null eloquent query check is any column is null and the where not null query check if any column is not null. We will shee ho we use the where null and where not null query in single or multiple columns in laravel.

Meaning about whereNull() and whereNotNull() in laravel:

1. IS NULL = whereNull()

2. IS NOL NULL = whereNotNull()

Laravel Where Null Query

The whereNull() helps us to get data with null values from database.

SQL Query:

SELECT * FROM users
  WHERE email_verified_at IS NULL;

Laravel Query:

User::whereNull('email_verified_at')
	->get();

or:

DB::table('users')
	->whereNull('email_verified_at')
	->get();

Laravel Where Not Null Query

The whereNotNull() helps us to get data with not null values from database.

SQL Query:

SELECT * FROM users
  WHERE email_verified_at IS NOT NULL;

Laravel Query:

User::whereNotNull('email_verified_at')
	->get();

Or:

DB::table('users')
	->whereNotNull('email_verified_at')
	->get();

laravel whereNull multiple columns

$posts = Post::where(function($q){
   return $q->whereNull('col_1')
       ->orWhereNull('col_2')
       ->orWhereNull('col_3');
})->where('status', 1)->get();

Or

$posts = Post::orWhereNull('col_1') 
   ->orWhereNull('col_2')
   ->orWhereNull('col_3')
   ->where('type', 1)
   ->get();

laravel whereNotNull multiple columns

$posts = Post::where(function($q){
   return $q->whereNotNull('col_1')
       ->whereNotNull('col_2')
       ->whereNotNull('col_3');
})->where('status', 1)->get();

Or

$articles = Article::orWhereNotNull('col_1') 
   ->orWhereNotNull('col_2')
   ->orWhereNotNull('col_3')
   ->where('type', 1)
   ->get();

So, today you have learned how to use whereNull and whereNotNull in laravel.

1 thought on “Laravel whereNull and whereNotNull Eloquent Query Example”

Leave a Comment