Laravel whereIn Eloquent Query Example

Laravel whereIn Eloquent Query Example

In this laravel wherein eloquent tutorial, you will learn everything about laravel eloquent whereIn() method to creating a query with model and builder.

In Laravel, the whereIn() method is an eloquent query builder function that allows you to filter query results based on a specified array list of values. This method is particularly useful when you want to retrieve records that match any of the values provided in the array list, which acts like an “OR” condition for multiple values or arrays.

The syntax for using whereIn() is straightforward:

$matchingRecords = Model::whereIn('column_name', $arrayOfValues)->get();

Here, Model represents the Eloquent model you’re querying, 'column_name' is the name of the column you want to compare the values against, and $arrayOfValues is an array containing the values you want to check for.

Here are a few examples to help you understand how to use the whereIn query in Laravel:

Example 1: wherein Query Using Simple SQL Query:

SELECT *  FROM users  WHERE id IN (10, 15, 18) 

In this query, the data will be get from the DB table. Whose ids will be 10, 15, 18.

Example 2: Retrieving Users by IDs using Eloquent Wherein Query

Let’s say you have a users table and you want to retrieve users by their IDs. You have an array of IDs you’re interested in:

$userIds = [1, 3, 5, 7];
$users = User::whereIn('id', $userIds)->get();

This will fetch users whose IDs match any of the values in the $userIds array.

Example 3: Filtering Posts by Categories Using Wherein

Suppose you have a posts table with a category_id column, and you want to retrieve posts from specific categories. You have an array of category IDs you’re targeting:

$categoryIds = [2, 4, 6];
$posts = Post::whereIn('category_id', $categoryIds)->get();

This query will return posts that belong to any of the specified categories.

Example 4: Eloquent Wherein Query Using Laravel Model With Different Column Name

public function index()
    $data= User::whereIn('name', ['john','dam','smith'])->get();

Example 5: Selecting Orders by Status

Assume you have an orders table with an order_status column indicating the status of an order. You want to retrieve orders with certain statuses:

$statuses = ['pending', 'processing'];

$orders = Order::whereIn('order_status', $statuses)->get();

Here, the query will fetch orders with either a “pending” or “processing” status.


Laravel wherein query example tutorial, you have learned how to use laravel wherein() method with eloquent query builder and model.

Recommended Laravel Posts


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *