ItSolutionStuff.com

Laravel Eloquent WhereIn Query Example

By Hardik Savani • April 16, 2024
Laravel

Today's topic is how to use wherein query in laravel eloquent. How works laravel model wherein array query. i will give you simple example of wherein laravel query builder. you can use laravel eloquent where in array.

you can easily use wherein eloquent method in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

If you need to use sql wherein query in laravel then you can use with array. Laravel provide wherein() to use sql wherein query. in wherein() 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 wherein query in laravel:

whereIn(Coulumn_name, Array);

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

Example 1:

SQL Query:

SELECT *

FROM users

WHERE id IN (4, 5, 6)

Laravel Query:

public function index()

{

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

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

->get();

dd($users);

}

Example 2:

Here is another example of wherein 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("*")

->whereIn('id', $myArray)

->get();

dd($users);

}

Example 3:

public function index()

{

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

->whereIn('name', ['Hardik', 'Vimal', 'Harshad'])

->get();

dd($users);

}

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 Sum Query with Where Condition Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Eloquent Left Join Where Null Condition Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to Use If Condition in Laravel Select Query?

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel WhereIn() and WhereNotIn() with Subquery Example

Read Now →

Laravel Select with Sum Query Example

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

How to Get Last Executed Query in Laravel?

Read Now →

Laravel Query Builder Where Exists Example

Read Now →