Laravel’s Eloquent ORM ( Object Relational mapping ) is a powerful tool for working with databases in our Laravel applications. It provides an intuitive interface for defining relationships between models and querying related data.
In this blog post, we’ll dive deep into three important Eloquent methods: has(), with(), and whereHas(). We’ll explore what they mean, how they work, and when to use them, complete with practical examples.
The has() method is used to query for the existence of related models. It allows us to filter results based on the presence or absence of related records.
When we use has()relation, we’re essentially asking, “Does this model have any related models in another table?”
The has() method adds a where exists clause to your query. It checks for the existence of at least one related record that matches the given conditions.
Lets understand it by diving into one of the example of use case of the has() clause,
Say we have a Book model and a Review model, where a book can have multiple reviews.
Now, we want to find all books that have at least one review:
$reviewedBooks = Book::has('reviews')->get();
This query will return all books that have at least one associated review.
Using above, we can also specify a minimum count.
$popularBooks = Book::has('reviews', '>=', 10)->get();
This will return books that have 10 or more reviews.
The with() method is used for eager loading related models. It helps optimize your queries by reducing the number of database queries needed to retrieve related data.
When you use with(), we’re telling Eloquent That, “I know I’m going to need this related data, so please load it all at once.”
The with() method performs a separate query to fetch the related models and then associates them with the main models in memory. This reduces the “N+1 query problem” where we might otherwise end up running a separate query for each related model.
Let’s understand it by diving into an Example,
Say use an Author model that has many Book models:
$authorsWithBooks = Author::with('books')->get(); foreach ($authorsWithBooks as $author) { echo $author->name . ' has written ' . $author->books->count() . ' books.'; }
This will load all authors and their books in just two queries, rather than performing a separate query for each author’s books.
//get all the Authors Select * from authors //Get The books based on Authors Select * from books where authors.id IN (1,2,3,4,5,6.......)
The whereHas() and orWhereHas() method combines the functionality of has() with the ability to add custom constraints on the related models.
When we use whereHas(), we’re asking, “Does this model have any related models that meet specific conditions?”
The whereHas() method adds a where exists clause to our query, similar to has(), but allows us to specify additional conditions on the related models.
Let’s understanding it through an example, say we want to find all books that have received a 5-star review:
$highlyRatedBooks = Book::whereHas('reviews', function ($query) { $query->where('rating', 5); })->get();
This query will return all books that have at least one review with a 5-star rating.
Based On above examples, we can combine all this methods to get a very powerful queries.
$books = Book::with('author') ->whereHas('reviews', function ($query) { $query->where('rating', '>=', 4); }) ->has('categories') ->get();
This above query will results in following:
Understanding and effectively using has(), with(), and whereHas() in Laravel’s Eloquent ORM can significantly improve our ability to write efficient and expressive database queries. By leveraging these methods, we can optimize our application’s performance and write cleaner, more maintainable code.
1.has(): Filters results based on the existence of related models, allowing you to quickly find records with (or without) specific relationships.
2. with(): Implements eager loading, dramatically reducing the number of database queries and improving your application’s performance.
3. whereHas(): Combines relationship checking with custom conditions, enabling complex, precise queries on related data.