In this tutorial you will learn about the Laravel where Not In Eloquent Query Example and its application with practical example.
In this Laravel where Not In Eloquent Query Example tutorial, I’ll show you how to use laravel eloquent whereNotIn() method to fetch data from laravel model using laravel query builder.
Laravel whereNotIn Eloquent Query Example
In this article, we will provides you simple example to demonstrate the use of laravel whereNotIn method on query builder and mode.We will also use example to show how to use laravel eloquent whereNotIn with arrays.
whereNotIn method syntax
The Laravel whereNotIn method is mainly used to skip some records when fetching records from the database table.
1 |
whereNotIn(Coulumn_name, Array); |
Here,
column_name:- Your database table column name.
Array: – array with comma-separated values that you want to skip.
whereNotIn Query Using Simple SQL Query
1 |
SELECT * FROM users WHERE id NOT IN (10, 15, 18) |
The above sql query will fetch data from users table except the user with id 10, 15, 18.
Eloquent WhereNotIn Query Using Query Builder
In this example we will be fetching users data using laravel whereNotIn clause with query builder.
1 2 3 4 5 6 7 8 |
public function index() { $data = DB::table('users') ->whereNotIn('id', [10, 15, 18]) ->get(); dd($data); } |
Eloquent WhereNotIn Query Using Laravel Model
In this example we will be fetching users data using laravel whereNotIn clause with laravel model.
1 2 3 4 5 6 |
public function index() { $data= User::whereNotIn('id', [10, 15, 18])->get(); dd($data); } |
Eloquent WhereNotIn Query Using Laravel Model With Different Column Name
In this example we will be fetching users data using laravel whereNotIn clause with laravel column names.
1 2 3 4 5 6 |
public function index() { $data= User::whereNotIn('name', ['john','dam','smith'])->get(); dd($data); } |