In this tutorial you will learn about the Laravel Where Null and Where Not Null Query and its application with practical example.
In this Laravel where Null and where Not Null example tutorial I’ll show you how to use whereNull() and whereNotNull() methods in laravel. In this tutorial you will learn to use whereNull() and whereNotNull() methods for creating laravel eloquent queries. In this article I’ll share different use case of the whereNull() and whereNotNull() methods in laravel. In this Laravel where Null and where Not Null tutorial you will learn about the syntax and the use of whereNull and whereNotNull eloquent methods.
Laravel whereNull method syntax
Here is the general syntax of the laravel whereNull method:
1 |
whereNull('columnName'); |
Laravel whereNotNull method syntax
Here is the general syntax of the laravel whereNotNull method:
1 |
whereNotNull('columnName'); |
Laravel whereNull Query
In this example query we will fetch data from database table for the users where the name field empty. In this query we will demonstrate the use of whereNull Query. The laravel whereNull Query can be used as following:
1 2 3 4 5 6 |
public function index() { $users = User::whereNull('name')->get(); dd($users); } |
The above given whereNull query you will get the following SQL query:
1 |
SELECT * FROM users WHERE name IS NULL; |
Laravel whereNotNull Query
In this example query we will fetch data from database table for the users where the email_verified_at field is not empty in DB table:. In this query we will demonstrate the use of whereNotNull Query. The laravel whereNotNull Query can be used as following:
1 2 3 4 5 6 |
public function index() { $users = User::whereNotNull('email_verified_at')->get(); dd($users); } |
The above given whereNull query you will get the following SQL query:
1 |
SELECT * FROM users WHERE email_verified_at IS NOT NULL; |