Laravel WhereNotIn Query Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

This article will provide you lesson of where not in laravel query builder. we will write where not in query in laravel eloquent. it is very simple lesson of wherenotin in laravel application. you can see bellow where not in in laravel eloquent.

you can simple use wherenotin eloquent method in laravel 6, laravel 7 and laravel 8.

If you need to use sql wherenotin query in laravel then you can use with array. Laravel provide wherenotin() to use sql wherenotin query. in wherenotin() we just need to pass two argument one is column name and another if array of ids or anything that you want.

You can see bellow syntax on wherenotin query in laravel:

whereNotIn(Coulumn_name, Array);

Now i will give you three lesson of how to use wherenotin query in laravel application. So let's see those lesson how it works.

Lesson 1:

SQL Query:

SELECT *

FROM users

WHERE id NOT IN (4, 5, 6)

Laravel Query:

public function index()

{

$users = User::select("*")

->whereNotIn('id', [4, 5, 6])

->get();

dd($users);

}

Lesson 2:

Here is another lesson of wherenotin query. You can work with comma separated string value. you can work as like bellow:

public function index()

{

$myString = '1,2,3';

$myArray = explode(',', $myString);

$users = User::select("*")

->whereNotIn('id', $myArray)

->get();

dd($users);

}

Lesson 3:

public function index()

{

$users = DB::table('users')

->whereNotIn('name', ['Sagor', 'Vimal', 'Harshad'])

->get();

dd($users);

}

thank you very much for following up with me.

Recent Posts

Related Posts