Laravel Eloquent whereNull() & whereNotNull() Query

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. ?