Laravel where not null and where null with eloquent query example

Introduction

In this tutorial, we will learn how to query where null and where not null with eloquent in Laravel. I will use the raw query and convert into laravel eloquent.

For example:

Let's assume we have a table called the post with the following values.

Post Table

Id title post_description
1 test title this is test description
2 null this is 2nd test description
3 2nd title null
 

Where Not Null Query

SQL raw query

Select * from posts 
  where title IS NOT NULL;

Laravel eloquent query

DB::table('posts')
	->whereNotNull('title')
	->get();

Where Null Query

SQL raw query

select * from posts
  where title IS NULL;

Laravel eloquent query

DB::table('posts')
	->whereNull('title')
	->get();