Considering the basic example Has Many Through relationship in the Laravel guide, I'm trying to query directly with eloquent, countries with posts from type 100 (just an example).
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
type_id - integer
type_id in posts is my peculiar case..
So, I created a relationship in the country model that creates a relation with posts through users. The result that I want to achieve is a list of countries and within each country the posts created by users of that country.
//Country.php
public function posts()
{
return $this->hasManyThrough(
'Post', 'User', 'country_id', 'user_id'
);
}
I was able to replicate the result I want to achieve in other ways, but I would like to know why the eloquent query below does not return the expected result:
$postsGroupedByCountries = Country::with([
'posts' => function ($query) {
$query->where('type_id', 100);
}
])->get();
The return of the query are several countries with all posts without data. What is the correct way to do this query with Laravel Eloquent?
First try:
$postsGroupedByCountries = Country::whereHas('posts', function ($query) {
$query->where('type_id', 100);
})->get();
Second try:
$postsGroupedByCountries = Country::whereHas('posts', function ($query) {
$query->where('type_id', 100)->groupBy('posts.id');
})->get();
Third try:
$postsGroupedByCountries = Country::whereHas('posts', function ($query) {
$query->where('type_id', 100);
})->get();
$postsGroupedByCountries = $postsGroupedByCountries ->map(function($country){
$country->load('posts');
return $country;
});
And I've already tried inside the load to use a closure with where as well. But it also did not work. The result is the same.