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
How To Create Custom Middleware In Laravel
How To Create Custom Middlewar...

In this article, we will give you information about middleware and we will see how to create custom middleware in l...

Read More

Aug-24-2020

How to Upgrade from Angular 14 to Angular 15
How to Upgrade from Angular 14...

As a developer, keeping up with the latest advancements in Angular is crucial to stay ahead in the rapidly evolving worl...

Read More

May-31-2023

How To Remove index.php From URL In Laravel 9
How To Remove index.php From U...

If you're a developer, you're likely to have frustration with "index.php" cluttering up your website&#...

Read More

Jan-13-2023

Datatables Show And Hide Columns Dynamically In jQuery
Datatables Show And Hide Colum...

In this article, we will see how to hide and show columns in datatable in jquery. This example shows how you can ma...

Read More

Jun-07-2022