Laravel 9 whereNull / whereNotNull Query Example

Websolutionstuff | Oct-18-2022 | Categories : Laravel

In this article, we will see the laravel 9 whereNull / whereNotNull query example. In laravel 9 additional where clauses provide many built-in where methods. So, we will learn how to write queries for whereNull and whereNotNull methods in laravel 9. Also, laravel 9 provides the same method as orWhereNull and orWhereNotNull.

The whereNull() method is used to check the value of the given column is NULL and The whereNotNull() method is used to check the value of the given column is not NULL. Also, we will see examples of SQL query and syntax of IS NULL and IS NOT NULL.

So, let's see a whereNull query in laravel 9 and a whereNotNull query in laravel 9.

Syntax of IS NULL in SQL

The IS NULL is used to check the empty value.

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

 

Example 1:

In this example, we will retrieve all customers with a NULL value in the Address field. 

SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;

 

 

Syntax of the whereNull()

 In laravel whereNull() function add a column to check the condition.

whereNull(Column Name);

 

Example 2:

In this example, we will retrieve all records with the updated_at column value being NULL.

$users = DB::table('users')
                ->whereNull('updated_at')
                ->get();

 

Syntax of IS NOT NULL in SQL

The IS NOT NULL is used to check the non-empty value.

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

 

 

Example 3:

In this example, we will retrieve all customers with a value in the Address field. 

SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;

 

Syntax of the whereNotNull()

  In laravel whereNotNull() function add a column to check the condition.

whereNotNull(Column Name);

 

Example 4:

In this example, we will retrieve all records from the user's table with the updated_at column value not NULL.

$users = DB::table('users')
                ->whereNotNull('updated_at')
                ->get();

 


You might also like:

Recommended Post
Featured Post
How To Send Mail Using Gmail In Laravel 9
How To Send Mail Using Gmail I...

In this article, we will see how to send mail using gmail in laravel 9. we will learn laravel 9 to send mail u...

Read More

Aug-03-2022

Laravel Authentication Using Breeze
Laravel Authentication Using B...

In this article, we will share you new information about laravel authentication using a breeze. Laravel Breeze...

Read More

Feb-05-2021

Reseller Hosting Myths to Know and Unfollow
Reseller Hosting Myths to Know...

If you work in the web hosting sector, you're probably acquainted with the term reseller hosting. Resellers make up...

Read More

Apr-07-2022

Laravel 9 Form Validation Example
Laravel 9 Form Validation Exam...

 In this tutorial, we will see laravel 9 form validation example. For any incoming data, we need to validate i...

Read More

Feb-12-2022