Laravel Eloquent WhereNotIn Method Query Example

2357 views 11 months ago Laravel

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.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]