ItSolutionStuff.com

Laravel WhereIn() and WhereNotIn() with Subquery Example

By Hardik Savani • April 16, 2024
Laravel MySql

As you know laravel is very popular php framework in today. So every point will be require for database related and that important. In this post i would like to share with you how to use subquery with whereIn and whereNotIn clause from scratch.

We may sometimes require to use select query with where in or where not in clause in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11. we can simply make query using MySQL. But we don't know how to use it on laravel query builder. However, In this example we will learn how to convert mysql query into laravel query builder of wherein with subquery. This example is from scratch, so i will give you first mysql query and then we will convert it into laravel query builder.

In this example i have to tables as listed bellow:

1) users

2) invite_users

Where In Query

In first query, basically we require users that only in invite_users table. we can normally retrieve using bellow sql query:

SQL Query:

SELECT * FROM `users`

WHERE `id` IN (

SELECT `user_id` FROM `invite_users`

)

As bellow we can convert for laravel application.

Laravel Query:

$users = DB::table("users")->select('*')

->whereIn('id',function($query){

$query->select('user_id')->from('invite_users');

})

->get();

Where Not In Query

In second query, basically we require users that not in invite_users table. we can normally retrieve using bellow sql query:

SQL Query:

SELECT * FROM `users`

WHERE `id` NOT IN (

SELECT `user_id` FROM `invite_users`

)

As bellow we can convert for laravel application.

Laravel Query:

$users = DB::table("users")->select('*')

->whereNOTIn('id',function($query){

$query->select('user_id')->from('invite_users');

})

->get();

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Where Clause with Function Query Example

Read Now →

Laravel Sum Query with Where Condition Example

Read Now →

Laravel Group By with Min Value Query Example

Read Now →

Laravel Eloquent orWhereHas() Condition Example

Read Now →

Laravel Search Case Insensitive Query Example

Read Now →

Laravel Case Sensitive Query Search Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Laravel Eloquent Where Query Examples

Read Now →

Laravel - How to use Subquery in Select Statement?

Read Now →

Laravel - whereDate(), whereMonth(), whereDay() and whereYear() Example

Read Now →

Laravel Where Clause with date_format() Example

Read Now →

Laravel Eloquent Group By with Multiple Columns Example

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

Laravel Where Clause with MySQL Function Example

Read Now →

Laravel Query Builder Where Exists Example

Read Now →

Laravel Join with Subquery in Query Builder Example

Read Now →