Back to Blog 3 min read

How to Use WhereHas In Laravel

Hi readers, I was using WhereHas today in some tasks. So, I decided let's share what it is. If you're building a Laravel application that involves wo...

Gurpreet Kait

Gurpreet Kait

Author

Hi readers, I was using WhereHas today in some tasks. So, I decided let's share what it is. If you're building a Laravel application that involves working with related models, you'll likely need to perform queries that filter models based on the existence or properties of related models. One of the most powerful and useful methods for performing these kinds of queries is whereHas. In this post, we'll cover what whereHas it is, how it works, and some practical examples.

What is WhereHas?

whereHas is a method available on Laravel's query builder that allows you to filter models based on the existence of related models that match certain conditions? This is especially useful when you want to retrieve models with at least one related model that meets certain criteria.

How does whereHas work?

The whereHas the method takes two arguments: the name of the relationship method on the current model, and a closure that defines the query to be executed on the related models. Here's an example:

$posts = Post::whereHas('comments', function ($query) {
    $query->where('body', 'like', '%important%');
})->get();

In this example, we're retrieving all Post models that have at least one related Comment model whose body the column contains the word "important". Here's how this query works:

  1. We call the whereHas method on the Post model, passing in the name of the comments relationship method as the first argument.
  2. We provide closure as the second argument to whereHas, which defines the query to be executed on the related Comment models.
  3. Inside the closure, we call the where method on the $query object (which is an instance of Laravel's query builder for the Comment model), filtering the Comment models based on the body column.

Note that whereHas doesn't actually retrieve any related models - it simply filters the main model based on the existence of related models that match the specified conditions.

You can read about Eloquent Relationship Methods On Official Website: Laraveldocs

Practical Examples

Let's look at some practical examples of how to use whereHas in Laravel.

Example 1: Retrieving Posts with Comments

Suppose you have a Post the model that has a comments relationship method is defined as follows:

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

You can use whereHas to retrieve all Post models that have at least one related Comment model:

$posts = Post::whereHas('comments')->get();

In this example, we're not providing closure to whereHas, which means that we're not filtering the related Comment models in any way. This query will simply retrieve all Post models that have at least one related Comment model.

Example 2: Retrieving Posts with Specific Comments

Suppose you want to retrieve all Post models that have at least one related Comment model with the word "important" in its body column:

$posts = Post::whereHas('comments', function ($query) {
    $query->where('body', 'like', '%important%');
})->get();

This query uses a closure to filter the related Comment models based on their body columns. We're using the like operator to perform a case-insensitive search for the word "important" anywhere in the body column.

Read Also : TIps to optimize your laravel web app

Conclusion

In conclusion, whereHas is a powerful method in Laravel that allows you to filter models based on the existence or properties of related models. It's useful when you want to retrieve models that have at least one related model that meets certain criteria. I hope this article will help you.