Laravel whereIn and whereNotIn Query Example

In this tutorial, we will explore the use of Laravel's whereIn and whereNotIn query methods, suitable for Laravel 8, 9, and 10. The Laravel query builder equips you with a range of powerful tools for efficient data filtering in your applications.

The whereIn method allows you to verify whether a specific column's value is present within a given array, while the whereNotIn method checks for the absence of a given column's value in the provided array. You can also utilize Laravel's whereIn and whereNotIn with subqueries for more advanced filtering.

Let's dive into practical examples of how to use whereIn and whereNotIn queries in Laravel, applicable to versions 8, 9, and 10, as well as earlier versions such as Laravel 6 and Laravel 7.

Syntax:

whereIn(Coulumn_name, Array)

 

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);                    
}

 

Conclusion:

In conclusion, we've explored the versatile whereIn and whereNotIn query methods in the context of Laravel, with applicability to versions 8, 9, and 10, as well as earlier versions like Laravel 6 and 7. These methods provide powerful tools for filtering data from your database efficiently.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS