Site Admin
Site Admin Founder of MeaningArticles
707 Views

Laravel Eloquent WhereNotIn Query Examples

Hello Dev.

Laravel where not in query example educational. right here, you may learn how to use laravel whereNotIn() eloquent technique to implementing a query with eloquent model and query builder in laravel.

This article presents you easy examples of where not in Laravel query Builder. And in addition to how to use Laravel Eloquent WhereNotIn with arrays.

Syntax

whereNotIn(Coulumn_name, Array);


Example 1:

whereNotIn Query Using Simple SQL Query

SELECT *  FROM users  WHERE id NOT IN (11, 14, 19)

In this SQL query, the data will not be get from the DB table. Whose ids will be 11, 14, 19.


Example 2:

Eloquent WhereNotIn Query Using Query Builder

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


Example 3:

Eloquent WhereNotIn Query Using Laravel Model

public function index()
{
    $data= User::whereNotIn('id', [11, 14, 19])->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);                    
}

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.