Laravel 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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