Laravel where Not In Eloquent Query Example - Yudhy Network

Share on:

Yudhy Network - Laravel where not in query example. Here, you will learn how to use laravel whereNotIn() eloquent method to building a query with model and query builder

Laravel where not in query example tutorial. Here, you will learn how to use laravel whereNotIn() eloquent method to implementing a query with eloquent model and query builder in laravel.

This tutorial provides you simple examples of where Not In Laravel Query Builder. 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.

Recommended:- Laravel – whereIn Eloquent Query Example

Example 2: Eloquent WhereNotIn Query Using Query Builder

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

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 implemeting a queries in laravel with eloquent model and query builder.

Recommended Laravel Posts

Recommended:- Laravel Multiple Where Conditions Example
Recommended:- Laravel Eloquent whereRaw Query Example
Recommended:- Laravel where Like Query Example
Recommended:- Laravel where Between Dates Eloquent Query
Recommended:- Laravel WhereHas Eloquent Example
Recommended:- Laravel where Not Between Query Example
Recommended:- Laravel whereExists and where Not Exists Query Example
Recommended:- Laravel Pluck Method Example
Recommended:- Laravel Where Null and Where Not Null Query
Recommended:- Laravel Group by Example
Recommended:- Laravel Order by Example
Recommended:- Laravel InsertOrIgnore Example

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus