Laravel whereIn and whereNotIn Query Example

Websolutionstuff | Jan-16-2021 | Categories : Laravel PHP

In this article, we will see the laravel whereIn and whereNotIn query examples. laravel query builder provides many different types of queries to filter data from databases. The whereIn method verifies that a given column's value is contained within the given array and the whereNotIn method verifies that the given column's value is not contained in the given array.

Also, you can check if the value exists or not using the whereIn function in laravel 6, laravel 7, and laravel 8. You may create whereIn and whereNotIn subquery in laravel 7/8.

So, let's see laravel 7/8 wherein the query and SQL in the query example.

Syntax:

whereIn(Coulumn_name, Array)

 

Example: 

SQL Query

SELECT * FROM students WHERE roll_no IN (1,2,3) 

 

Laravel whereIn Query

public function index()
{
    $students = Student::select("*")
                ->whereIn('roll_no', [1,2,3])
                ->get();
  
    dd($students);                    
}

 

Laravel whereNotIn Query

public function index()
{
    $roll_no = '1,2,3';
    $array1 = explode(',', $roll_no);
    
    $student = Student::select("*")
                    ->whereNotIn('roll_no', $array1)
                    ->get();
  
    dd($student);                    
}

 

Recommended Post
Featured Post
Laravel 10 Desktop and Mobile User Agent Parser
Laravel 10 Desktop and Mobile...

Discover the magic of Laravel 10's Desktop and Mobile User Agent Parser! With the help of the jenssegers/agent packa...

Read More

Nov-24-2023

Github And Git Commands
Github And Git Commands

GitHub, Inc. is a United States-based global company that provides hosting for software development and version control...

Read More

Jul-15-2020

Laravel 8 Export Buttons In Datatables Example
Laravel 8 Export Buttons In Da...

In this article, we will see an example of laravel 8 export buttons in datatables. If you want to export data...

Read More

Oct-14-2020

7+ Laravel tips: DB Models and Eloquent (2023)
7+ Laravel tips: DB Models and...

Welcome to our latest guide on mastering Laravel's DB Models and Eloquent, updated for 2023. If you're a web dev...

Read More

Nov-08-2023