I'm trying to get a model and also add filter to one of it's hasMany relationships.
Let's say I have two models. One of is Location
and other one is the LocationAnalytic
.
Location
model hasMany LocationAnalytic
class. So when I want to retrieve a Location by Id, it returns all of the Location
data and also returns a LocationAnalytic
data as array. In my case a Location model can be related to thousands of LocationAnalytic model.
My need is to get the Location
model but also get only related LocationAnalytic
data that has filtered by the given filters. By this way I will get the same Location
data and also get only couple of LocationAnalytic
data with it in the same response.
It is similar to whereHas directive but it will not check for existence, it will only filter relationship of a model.
My Query will be like the following;
query($id:Int!) {
location (id: $id) {
id
parent_id
name
admin_level
country
analytic {
id
category_id
total_listings_count
new_listings_count
expired_listings_count
created_at
}
}
}
And my filters will something like the following
{
"location_idid": 100075,
"analytics": {
"AND": [
{ "column": "CATEGORY_ID", "operator": "EQ", "value": 390005 }
]
}
}