Laravel Eloquent WhereNull() and whereNotNull() Example

Do you know how to use WhereNull() and whereNotNull() Laravel Eloquent.

In this post, I will explain how to use Laravel Eloquent  WhereNull() and whereNotNull().

you can use WhereNull() and whereNotNull()  with laravel 6, laravel 7, laravel 8 and laravel 9 application.

whereNull() will help you to get data with null values from the database.

whereNotNull() will help you to get data with not-null values from the database.

Lets look into the examples

How to use Laravel Eloquent WhereNull()

<?php
  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;
use App\Post;
  
class UserController extends Controller
{    
    public function index()
    {
        $posts = Post::whereNull('author')->get();  
        dd($posts);
    }
}

Let’s see the SQL Query

select * from `posts` where `author` is null



 

 

How to use Laravel Eloquent whereNotNull()

<?php
  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;
use App\Post;
  
class UserController extends Controller
{    
    public function index()
    {
        $posts = Post::whereNotNull('author')->get();  
        dd($posts);
    }
}

Let’s see the SQL Query

select * from `posts` where `author` is not null

 

Hope you like it.

Checkout More Awesome Laravel Tutorials

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to create Laravel Flash Messages

Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]

December 22, 2022

How to make Custom Artisan Command in Laravel

Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks […]

December 22, 2022

Tips for Laravel migrations

Laravel migrations are a powerful tool for managing and modifying your database schema in a structured and organized way. Here […]

December 22, 2022

Tricks and tips for Laravel blade templating

Welcome to our blog about tricks and tips for Laravel blade templating! Blade is the default templating engine for Laravel, […]

December 22, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now