Laravel whereNotIn Query Example; In this tutorial we will show you Laravel “Where NOT IN” eloquery query with eloquent model and query builder in laravel. The whereNotIn
method removes elements from the collection that have a specified item value that is not contained within the given array.
Laravel Eloquent “WHERE NOT IN” Query Example
whereNotIn() is similar to whereIn() except it negates the logical expression. Means, it matches the column against the list of values whether the column does not contain any value from the list. You can simply use wherenotin eloquent method in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 apps;
Syntax: Here is the whereNotIn query syntax that you can use in Laravel.
whereIn('column name', 'array');
whereNotIn() Laravel Query Example
The wherenotin() database query method takes two parameters. The first argument you pass column name, and the second value you supply is the array id.
SQL Query:
SELECT * FROM USERS WHERE id not in (1,2,3,4);
Laravel Eloquent Query:
$users = User::whereNotIn('id', [1,2,3,4]);
whereNotIn Exampl*e with Laravel Model
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$users = User::whereNotIn('id', [1, 3, 5])->get();
dd($users);
}
}
Laravel whereNotIn with Query Builder
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index()
{
$users = DB::table('users')->whereNotIn('id', [1, 3, 5])->get();
dd($users);
}
}
Laravel whereNotIn with Subquery
The following examples queries fetch data from users table, which is not available in the invite_users table by using whereNotIn subquery.
$users = DB::table("users")->select('*')
->whereNOTIn('id',function($query){
$query->select('user_id')->from('invite_users');
})
->get();
Read more: Laravel whereIn Eloquent Query Example
I hope this laravel whereNotIn query example works for your.
1 thought on “Laravel “Where Not In” Eloquent Query Example”