Laravel Eloquent whereNull() & whereNotNull() Query with Example

Published on July 31, 2020 29 sec read

In this article, I’m going to share how to use whereNull() and whereNotNull() in Laravel application. Let’s see the usage:

Table of Contents

  1. whereNull()
  2. whereNotNull()

whereNull()

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

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::whereNull('email_verified_at')->get();
  
        dd($users);
    }
}

whereNotNull()

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

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::whereNotNull('email_verified_at')->get();
  
        dd($users);
    }
}
That’s it. Thanks for reading. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.
Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Follow Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *