Laravel WhereNotIn with Examples

December 31, 2022
Laravel WhereNotIn with Examples

In this article, you will learn how you use laravel whereNotIn with examples. We will cover how to use laravel wherenot in and cover its few practical examples on it as well.

By the end of the article, you will be able to understand and know how to use where not in inside laravel.

Let’s get started!

What does where not in do?

In simple words, where not in is the opposite of where means it will not include anything which matches from the column name you specified.

The examples will give you a much more clear picture which I will be covering after discussing the syntax.

Syntax of whereNotIn in laravel

The syntax of where not in is pretty simple that first, you have pass the column name on which you will apply the where not property followed by the Array. The Array will have the values that you don’t want to get matched.

whereNotIn(column_name, Array);

Example 1:

Let’s say we have the users and we don’t want users whom names are john, methaw and alexey.

The SQL query of that will be like this:

Select *
 from users
 where name not in ('john','methaw','alexey'); 

And in laravel eloquent we can write in this way into the controller:

public function index()
    $all_users = User::select("*")

Example 2:

In this example, I will show you how to use wherenotin laravel with the DB class.

public function index()
    $all_users = DB::table('users')
                    ->whereNotIn('name', ['john','methaw','alexey'])


Hope you have learned today how to use the whereNotin in laravel eloquent with the examples.

Let me know if you have any questions.

See you next time!

Write a Reply or Comment

Your email address will not be published. Required fields are marked *