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 get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

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 […]

July 28, 2022

Laravel Array to JSON Response From Laravel Controller

Do you know how to make JSON responses from a Laravel array? As a developer someday we all have to […]

July 28, 2022

Digital Marketing Toolkit

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

Get Free Access Now