Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel whereIn query with example

Mastering Laravel’s whereIn Query with Examples

Aman Jain, January 18, 2022October 21, 2023

Are you struggling with Laravel’s whereIn query? In this comprehensive guide, you’ll learn how to harness the power of Laravel’s whereIn Eloquent builder for creating dynamic queries, subqueries, and more. Explore the multiple ways to use this versatile method and gain a deep understanding with practical examples.

Below examples will work with any version of laravel 5 whereIn, laravel 6 whereIn, laravel 7 whereIn, laravel 8 whereIn, laravel 9 whereIn and laravel 10 whereIn.

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

Syntax to use:

whereIn('COLUMN_NAME', Array);

//OR

whereIn('COLUMN_NAME',Function);

The SQL equivalent of the above query will be

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

//OR

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

As you can see i have written multiple way to use same method with different type of parameters. Now, let’s dive into some practical examples:

Example 1 – whereIn with array example

In this example, we’ll demonstrate a simple whereIn query with an array condition using 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("*")
                        ->whereIn("id", [1,2,3])
                        ->get();
      \\or
      DB::table('articles')->whereIn("id",[1,2,3])->get();

   }
}

Output will be :

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

Example 2 – whereIn with subquery using closure function

In this example, we’ll use a closure function to create a 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())
                              ->whereIn('category_id', ['223', '15'])
                              ->where('active', 1);
                         })
                        ->get();
      \\or
       DB::table('articles')
       ->whereIn('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` in (select article_type_id from article_categories where category_id in (223,15)  )

Example 3 – Multiple whereIn and orWhere condition

In this example, we’ll combine whereIn, orWhere, and subquery for more complex conditions

<?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->whereIn("cat_id",[2,4,6])->orWhere("status",1);
                        })
                        ->get();
   }
}

Output

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

Example 4 – Complicated Raw query in where

In this example we will use DB::raw to use complicated query in our laravel application. Sometimes we wanted to use aggregate function in laravel where so

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

     //
    $Article = Article::select("*")
                        ->where(\DB::raw(" in (select id from table)"))
                         ->get(); 
   }
}

Output

select * from `articles` where `id` in (select id from table) 

Related

Php Laravel laravelphpwherewherein

Post navigation

Previous post
Next post

Related Posts

Php Unique Validation in laravel 8

Unique validation on Update in Laravel with example

June 19, 2022November 17, 2023

In this article we will learn to handle the unique validation on update in laravel.As we know Laravel support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while…

Read More

How to print database query in laravel 8 ?

January 1, 2022February 22, 2024

Sometime to debug the MySQL query in laravel eloquent we need to print the raw MySQL query. In laravel we have multiple methods to print database query in laravel 8 and we can also print the query before executing it. Laravel eloquent method to print the query The first approach…

Read More
Laravel Image Conversion to WebP in the fly Laravel

Image Conversion to WebP in the fly Laravel

February 22, 2024March 16, 2024

Are you looking to optimize your web application’s image handling and delivery speed? In the world of web development Image Conversion to WebP in the fly Laravel is key, and one of the ways to achieve faster loading times for images is by using the WebP format. Laravel, a popular…

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