Laravel: whereIn & whereNotIn Eloquent Query with Example

By Parth Patel on Oct 07, 2020

Laravel Query Builder is an advanced interface for interacting with Database to perform various types of SQL Queries.

Today, we will learn how to use whereIn() and whereNotIn() query in Laravel Eloquent.

whereIn() Laravel Query with Example:

whereIn() is used to check whether column contains value from the array or list. Basically, it is used to match column against list of values.

SQL Query:

Select * from users where id in (1,2,3,4);

Laravel Eloquent Query:

Similar thing can be achieved in Laravel Eloquent using below:

$users = User::whereIn('id', [1,2,3,4])->get();

whereNotIn() Laravel Query with Example:

whereNotIn() is similar to whereIn() except it negates the logical expression. Means, it matches the column against the list of values whether the column does not contain any value from the list.

SQL Query:

Select * from users where id not in (1,2,3,4);

Laravel Eloquent Query:

$users = User::whereNotIn('id', [1,2,3,4]);

So, this is simple but very useful eloquent query helper which can be combined in very powerful and complex database queries.