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 integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

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

Get Free Access Now