DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel whereIn and whereNotIn Query Example

In this tutorial, 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 make a laravel wherein subquery and laravel where not in the subquery.

So, here we will see the laravel whereIn and laravel whereNotIn query with example. Also, you use whereIn and WhereNotIn in laravel 6, Laravel 7 and Laravel 8.

Syntax :

whereIn(Coulumn_name, Array)
Enter fullscreen mode Exit fullscreen mode

SQL Query :

SELECT * FROM students WHERE roll_no IN (1,2,3) 
Enter fullscreen mode Exit fullscreen mode

Read Also : Laravel whereBetween Query Example


Laravel whereIn Query :

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

    dd($students);                    
}
Enter fullscreen mode Exit fullscreen mode

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);                    
}
Enter fullscreen mode Exit fullscreen mode

You might also like :

Top comments (0)

Why You Need to Study Javascript Fundamentals

The harsh reality for JS Developers: If you don't study the fundamentals, you'll be just another β€œCoder”. Top learnings on how to get to the mid/senior level faster as a JavaScript developer by Dragos Nedelcu.