Laravel WhereNotIn query method example

Laravel WhereNotIn query method is opposite method of WhereIn query method. When you want to get records in which specified field value is not from given array. Laravel whereNotIn() method uses SQL WHERE NOT IN statement.

In this tutorial, we are going to see Laravel whereNotIn eloquent query method example. This method compares field value with given array. Laravel whereNotIn() method works well with Eloquent as well as query builder. Let's see both example for whereNotIn() method. Laravel whereNotIn method works well in get(), update() or delete() method.

whereNotIn() method in Laravel Eloquent

In this example, we will use User model to get users which doesn't have ids from given array.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $users = User::whereNotIn('id', [1, 3, 5])
        ->get();
    
    dd($users);
}

whereNotIn() method in query builder

In this example, we will use DB facade to return users which doesn't have ids from given array.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $users = \DB::table('users')
        ->whereNotIn('id', [1, 3, 5])
        ->get();
    
    dd($users);
}

I hope it will help you.

Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment