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

Php Laravel 11 Ajax Image Upload with form with example

Laravel 11 Ajax Image Upload with form with example

July 16, 2024July 16, 2024

In this tutorial, I will demonstrate how to implement Laravel 11 Ajax Image Upload with form. This can be achieved using Laravel’s file and storage providers. In our previous articles, such as How to Upload image in Laravel 11 ? we explained how to upload images without Ajax. However, today…

Read More
Php Create Custom Directives in Laravel

How to create custom directive in Laravel 8 ?

November 11, 2021November 5, 2023

Laravel blade is powerful template engine which makes short tags directive and easy to reuse templates. Most of the templates restrict to use other language but blade gives more flexibility and we can use PHP as well in template. Directives start with prefix @ and accepts parameter. In this article…

Read More
Laravel Efficient and Secure Way to Write Raw Queries in Laravel

Efficient and Secure Way to Write Raw Queries in Laravel

July 1, 2024July 1, 2024

Laravel is a powerful and versatile PHP framework that simplifies many aspects of web development, including database interactions. While Laravel’s Eloquent ORM and query builder are incredibly useful, there are times when you need to write raw SQL queries for more complex operations. However, writing raw queries requires a careful…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version