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 set timezone dynamically and globally in Laravel

How to set timezone dynamically and globally in Laravel ?

September 20, 2022March 16, 2024

Laravel stores timezone configurations in config/app.php file and we can easily set timezone dynamically and globally in Laravel. Laravel usage carbon library to get the current date time and format, carbon also respects the timezone defined in config/app.php file. After php 5, php also introduced DateTime class to conduct the…

Read More
Php How to Copy Folder Recursively One Folder to Another in Laravel

How to Copy Folder Recursively One Folder to Another in Laravel ?

May 26, 2022May 26, 2022

In this article i will help you to copy the folders recursively from one folder to another in laravel .File System is used to perform the file system based operations like copy Folder Recursively. For this purpose laravel uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the…

Read More
Php Use Soft Delete to Temporary (Trash) Delete the Records in Laravel

Use Soft Delete to Temporary (Trash) Delete the Records in Laravel ?

June 17, 2022June 17, 2022

In laravel we use Soft Delete to Temporary (Trash) Delete the Records. Soft delete in laravel is use to hide the record from users by keeping the data in the database. There is many purpose of soft delete like deleting the user for admin but keep all the information in…

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

  • 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 Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version