Simplifying Data Filtering with Laravel’s ORM: Exploring the Power of whereHas()

Prasetyo Putra Pratama
Google Developer Indonesia
3 min readAug 30, 2023

In the realm of modern web development, data management plays a pivotal role in creating efficient and effective applications. Laravel, a popular PHP framework, offers a sophisticated Object-Relational Mapping (ORM) system that significantly simplifies data manipulation tasks. One of the key features within Laravel’s ORM is the whereHas() method, which allows developers to perform intricate data filtering with remarkable ease. In this article, we'll delve into the world of data filtration using the whereHas() method in the context of Laravel's ORM, focusing on a practical example involving filtering techniques applied to a hospital tools dataset.

Understanding the Basics of whereHas()

The whereHas() method is a versatile tool within Laravel's Eloquent ORM that enables you to filter query results based on the existence of related models. It is particularly useful when dealing with relationships between models, such as one-to-many or many-to-many relationships. The method accepts two parameters: the relationship name and a closure that defines the filtering criteria for the related model.

Exploring the Practical Example

Let’s dive into a practical scenario where we utilize the whereHas() method to filter hospital tools data based on specific criteria. Consider the following code snippet:

$user->whereHas(‘toolHospital’, function ($query) use ($filter) {
$query->whereHas(‘tool’, function ($query) use ($filter) {
$query->where(‘name’, ‘LIKE’, ‘%’ . $filter[‘name_tool’] . ‘%’);
});
});

In this example, we are working with a scenario where a user is attempting to filter hospital tools based on a certain tool name. Here’s a breakdown of the code:

  1. $user: This likely represents an instance of the user model.
  2. toolHospital: This is the relationship method that defines the connection between the user and hospital tools.
  3. tool: This represents the relationship between hospital tools and the actual tools themselves.

The code snippet applies a cascading filtering technique, where we filter tools based on their name, and then filter hospital tools based on the filtered tools.

Step-by-Step Explanation

  1. The outer whereHas() method is applied to the user model to filter hospital tools based on certain criteria.
  2. The closure within the outer whereHas() method is used to access the toolHospital relationship.
  3. Inside the inner closure, the whereHas() method is again employed to access the tool relationship within toolHospital.
  4. Finally, the where() method is used to apply the actual filtering criterion, searching for tools with names that match the pattern defined by $filter['name_tool'].

The Power of Eloquent ORM in Data Filtering

Laravel’s Eloquent ORM truly shines in scenarios like this. The code example showcases the elegance and efficiency of the ORM system. By utilizing nested whereHas() clauses, developers can effortlessly navigate through complex relationships and apply intricate filters without the need for convoluted SQL queries.

Conclusion

In the ever-evolving landscape of web development, efficiency and maintainability are paramount. Laravel’s whereHas() method, along with its broader Eloquent ORM capabilities, empowers developers to streamline data filtering processes. The example provided demonstrates how even intricate filtering requirements can be addressed elegantly through the power of Laravel's ORM, ultimately contributing to the creation of robust and feature-rich applications.

--

--

Prasetyo Putra Pratama
Google Developer Indonesia

Hi, I'm a Software Engineer and blockchain enthusiast, and I love to talk about the latest developments in technology