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

Published on July 31, 2020 29 sec read

HIRE US 🖐
We're available to do freelance project. Take a look at our services!

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

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

Follow

Leave a Reply

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