Friday, October 3, 2025
HomeLanguagesLaravel whereNotIn Eloquent Query Example

Laravel whereNotIn Eloquent Query Example

In Laravel, the whereNotIn() method is an Eloquent query builder method that is used to create WHERE NOT IN clauses for database queries. Eloquent is Laravel’s built-in ORM (object-relational mapping) which simplifies database interaction by allowing developers to work with database records as objects.

In this tutorial, you will learn how to use laravel whereNotIn() eloquent method to implement a query with model and query builder in laravel.

In this tutorial, you wil get simple examples of where Not In Laravel Query Builder is. And as well as how to use Laravel Eloquent WhereNotIn with arrays.

Suppose you want to skip some records when fetching records from the database table. For e.g. you have a users table and you do not want to get records whose user id’s 10, 15, 18. So you can use the eloquent WhereNotIn() method with a query.

The following syntax represents the whereNotIn eloquent method in laravel:

whereNotIn(Coulumn_name, Array);

Here,

  • Column_name:- Your database table column name.
  • Array: – Column’s value is not contained in the given array.

Let’s take look at some example of whereNotIn() eloquent method in laravel:

Example 1: whereNotIn Query Using Simple SQL Query:

SELECT *  FROM users  WHERE id NOT IN (10, 15, 18) 

In this SQL query, the data will not be get from the DB table. Whose ids will be 10, 15, 18.

Example 2: Eloquent WhereNotIn Query Using Query Builder

public function index()
{
    $data = DB::table('users')
                ->whereNotIn('id', [10, 15, 18])
                ->get();
  
    dd($data);                    
}

Example 3: Eloquent WhereNotIn Query Using Laravel Model

public function index()
{
    $data= User::whereNotIn('id', [10, 15, 18])->get();
  
    dd($data);                    
}

So far you have seen in the given example. Have used ids for scaping and getting data into db table. But now for example 4, we will fetch the data by using the DB table column name.

Example 4: Eloquent WhereNotIn Query Using Laravel Model With Different Column Name

public function index()
{
    $data= User::whereNotIn('name', ['john','dam','smith'])->get();
  
    dd($data);                    
}

Conclusion

In this where not in laravel query example, you have learned how to use laravel whereNotIn() eloquent method to implement a queries in laravel with eloquent model and query builder.

Recommended Laravel Posts

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS