Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel where not null query with example

How to use laravel where not null and where null query with example ?

Aman Jain, January 18, 2022January 18, 2022

In the initial stage of laravel learning you will find many challenges to build the query like where not null and where null so in this article I will show you to create where not null and where null query using laravel eloquent.

To create where not null in laravel eloquent we can use whereNotNull method which accepts 1 parameter column name.

Here is the simple syntax to use the laravel whereNotNull query

Syntax to use:

whereNotNull('COLUMN_NAME');

SQL of above query will be

select * from `table` where  `COLUMN_NAME` IS NOT NULL

So lets begin with simple examples

Example 1 – whereNotNull example

In this example i will show you simple whereNotNull

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")
                        ->whereNotNull("category_id")
                        ->get();
      \\or
      DB::table('articles')->whereNotNull("category_id")->get();

   }
}

Output will be :

select * from `articles` where `category_id` IS NOT NULL

Laravel where null query in eloquent

To create where null in laravel eloquent we can use wherNull method which accepts 1 parameter column name.

Here is the simple syntax to use the laravel whereNull query

Syntax to use:

whereNull('COLUMN_NAME');

SQL of above query will be

select * from `table` where `COLUMN_NAME` IS NULL

So lets begin with simple examples

Example 1 – whereNull example

In this example i will show you simple whereNull

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")
                        ->whereNull("category_id")
                        ->get();
      \\or
      DB::table('articles')->whereNull("category_id")->get();

   }
}

Output will be :

select * from `articles` where `category_id` IS NULL

Output

select * from `articles` where `category_id` in (select article_type_id from article_categories where category_id in (223,15)  )

Related

Php Laravel laravelmysqlnot nullphpwhere

Post navigation

Previous post
Next post

Related Posts

Devops install php 8 in ubuntu

How to install PHP 8.0 in Ubuntu 20.04 LTS ?

September 18, 2021November 5, 2023

In this tutorial we will learn to install PHP 8.0 on Ubuntu 20.04 LTS using PPA. PHP 8.0 is the latest version of php and it have many upgrades. Installation process is same as How to install PHP on Ubuntu? or How to Install multiple/different version of PHP? Step 1…

Read More
Php How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 9 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

Read More
Laravel Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version