Laravel Eloquent Query | Laravel WhereNotIn Query Example

Raviya Technical
1 min readSep 7, 2021

If you need to use SQL wherenotin query in laravel then you can use it with the array. Laravel provides wherenotin() to use SQL wherenotin query. in wherenotin() we just need to pass two arguments one is the column name and another is an array of ids or anything that you want.

You can see below syntax on wherenotin query in laravel:

whereNotIn(Coulumn_name, Array);

Now I will give you three examples of how to use wherenotin query in the laravel application. So let’s see those examples of how it works.

SQL Query

SELECT *
FROM users
WHERE id NOT IN (4, 5, 6)

Example 1:

Laravel Query

public function index(){$users = User::select("*")
->whereNotIn('id', [4, 5, 6])
->get();
dd($users);}

Example 2:

Here is another example of wherenotin query. You can work with a comma-separated string value. you can work as like bellow:

public function index(){$myString = '1,2,3';
$myArray = explode(',', $myString);
$users = User::select("*")
->whereNotIn('id', $myArray)
->get();
dd($users);}

Example 3:

public function index(){$users = DB::table('users')
->whereNotIn('name', ['Hardik', 'Vimal', 'Harshad'])
->get();
dd($users);}

--

--