in this tutorial, We’ll explore different ways to implement Eloquent whereIn in the Laravel application. This method allows you to filter query results based on a set of conditions.
Laravel’s Eloquent ORM (Object-Relational Mapping) is a powerful tool for interacting with databases conveniently.
The whereIn
method is helpful to get records out of an array or collection where a given column’s value matches filter criteria.
The whereIn method in Laravel Eloquent provides a convenient way to filter query results based on a set of values within a specified column. The WHEREIN is designed to match a column’s value against an array of conditions.
Syntax:
$emps = Employee::whereIn('column_name', [value1, value2, …])->get();
Parameters are:
Employee: This is the eloquent model name.
column_name: The column name where the value matches any of the values in the provided array.
Return: This method returns records from the employee table where the specified column’s value matches any values in the provided array.
Let’s take a simple example to filter records:
$emps = Employee::whereIn('id', [1, 2, 3])->get();
We are querying the “employees” table, and filtering records where the “id” column matches any of the values in the array [1, 2, 3].
We can filter multiple values based on condition using whereIn.
$depts = [1, 2]; $emps = Employee::whereIn('dept_id', $depts)->get();
It retrieves employees that belong to any of the selected departments.
The WHEREIN is also allows to passing array of string values. Let’s retrieve records of users with specific roles:
$roles = ['admin, 'user]; $emps = Employee::whereIn('role', $roles)->get();
in the above code, we are querying for employees whose “role” column matches any of the values in the $roles
array.
The whereIn method allows you to dynamically build queries based on user inputs.
$status = [ true ]; $emps = Employee::whereIn('status', $status); if ($request->has('emp_id')) { $emps->where('emp_id', $request->input('emp_id')); } $result = $posts->get();
You can also do mass updates and deletions using the whereIn method. You can pass multiple ids
to update or delete records from the table.
$emp_ids = [5, 8, 12]; Employee::whereIn('emp_id', $emp_ids)->update(['status' => false]);
in the above code, we are updating bulk records for column 'status'
.
You can also do the method chaining with the whereIn. You can create more complex queries to filter data.
$dept_ids = [1, 2, 3]; $salary = 5000; $emps = Employee::whereIn('dept_id', $dept_ids) ->where('salary', '>', $salary) ->get();
Laravel Eloquent allows developers to use Closure constraints within the WHEREIN clause. The simple example is:
$users = Employee::whereIn('emp_id', function ($query) { $query->select('emp_id') ->from('dept') ->where('dept_id', '=', 2); })->get();
Also checkout other Laravel 10 tutorials,
We have explored different scenarios to implement whereIn in your Laravel application. You can use it with dynamic conditions, subqueries, or Closure constraints. This method empowers developers to craft efficient and dynamic database queries.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More