Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to use laravel whereNotIn query with example

How to use laravel whereNotIn query with example ?

Aman Jain, October 15, 2022March 16, 2024

In this guide we will learn to create SQL not in query using laravel whereIn eloquent builder. whereNotIn laravel can used multiple ways to build the query one the of the feature of laravel eloquent is creating dynamic query based on condition and sub queries too.

To create where not in in laravel eloquent we can use whereNotIn method which accepts two parameter one is column name and other one is list of Array.

Here is the simple syntax to use the laravel whereNotIn query

Syntax to use:

whereNotIn('COLUMN_NAME', Array);

//OR

whereNotIn('COLUMN_NAME',Function);

SQL of above query will be

select * from `table` where  `COLUMN_NAME` not in  ('Array')

//OR

select * from `table` where  `COLUMN_NAME` not in  (select id from `table2')

As you can see i have written multiple way to use same method with different type of parameters. So lets begin tutorial with example

Example 1 – whereNotIn with array example

In this example i will show you simple whereNotIn with array condition with two parameters

<?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("*")
                        ->whereNotIn("id", [1,2,3])
                        ->get();
      \\or
      DB::table('articles')->whereNotIn("id",[1,2,3])->get();

   }
}

Output will be :

select * from `articles` where `id` not in (1,2,3) 

Example 2 – whereNotIn with subquery using closure function

In this example we will use closure function to create subquery.

<?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("*")
                        ->where("category_id", function($query){
                            $query->select('article_type_id')
                             ->from(with(new ArticleCategory)->getTable())
                              ->whereNotIn('category_id', ['223', '15'])
                              ->where('active', 1);
                         })
                        ->get();
      \\or
       DB::table('articles')
       ->whereNotIn('id', function($query)
         {
          return $query->select('article_type_id')
              ->from('article_categories')
              ->whereIn('category_id', ['223', '15']);
      })->get();

      
   }
}

Output

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

Example 3 – Multiple whereNotIn and orWhere condition

In this example we will use whereNotIn, orWhere and subquery.

<?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("*")
                        ->whereIn("id",[1,2,3])
                        ->where(function($query){
                            $query->whereNotIn("cat_id",[2,4,6])->orWhere("status",1);
                        })
                        ->get();
   }
}

Output

select * from `articles` where `id` not in (1,2,3) and (`cat_id` in (2,4,6) or `status` = 1)

Related

Php Laravel laravelphpwherewhereNotIn

Post navigation

Previous post
Next post

Related Posts

Php How to Add Google reCAPTCHA in Laravel 9 8 7 6 5

How to Add Google reCAPTCHA in Laravel 9 / 8 / 7 / 6 /5 ?

March 11, 2022March 11, 2022

Google reCAPTCHA used widely in may websites, in laravel its easy to use with third party package. Captcha is used to enhance the security of form. By adding the Captcha in laravel form we can prevent attackers to submit the form using the automated scripts and it adds an extra…

Read More
Php laravel validation

How to validate a form in Laravel 8 with example ?

October 4, 2021November 17, 2023

Laravel has inbuilt validation library to validate the input, post and get payload. Laravel has many validation like to validate the image, array, exist in database table, unique, regex etc. In this tutorial we are going to learn the validation on input for using Laravel validation library. We will use…

Read More
Php Ajax laravel Image Upload

Ajax laravel Image Upload with form with example

June 6, 2022November 6, 2023

In this tutorial i will show you to use Ajax laravel image Upload with form in laravel . This can be implement easily using the laravel file and storage providers. in our recent articles of How to Upload image with preview in Laravel 8 with example ? i explained to…

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