Laravel Where Not in Eloquent Example

Laravel Where Not in Eloquent Example

Laravel where not in the study of the sample questions. Here, you will learn how to use where not in laravel, laravel whereNotIn() is a great way to use query with art model and quiz builder in laravel.

This tutorial gives you simple examples of where not in laravel. And how to use laravel eloquent wherenotin Laravel whereNotIn with arrays.

Suppose you want to skip certain records when you download records in the data table. Eg. you have a user table and you do not want to get records of the user id of 10, 15, 20. So you can use the explicit WhereNotIn() method in question.

Read Also :  Laravel Insert Record using jQuery Ajax

You can see follow syntax on Where Not In query 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.

Example 1: where not in laravel Query Using SQL

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

Example 2: where not in laravel Query Using Query Builder

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

Example 3: where not in laravel Query Using Laravel Model

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

Example 4 : where not in laravel Query Using Laravel Model With Different Column Name

public function index()
{
 $users = User::whereNotIn('name', ['dharmik', 'sanket', 'fenil'])
        ->get();
        dd($users);
}

Also is another example of laravel wherenotin query. You can work with comma separated string value. you can work as such as follow:

public function index()
{
 $values = '10,15,20';
 $myArray = explode(',', $values);
 $users = User::select("*")
   ->whereNotIn('id', $myArray)
   ->get();
   dd($users);
}

I hope it can help you…

Read Also :  Laravel Arr flatten() function Example