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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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