Can't figure out how to design relational query
I have three tables
- categories
- products
- media
I want to retrieve products that belong to a particular category, along with the product category and all images. The results should be paginated and should be filtered by category_slug, organization_id.
Relationships:
1. Many products can belong to one category (products - categories = Many-to-one)
2. Many products can have many images (products - images = Many-to-Many)
I have tried:
Problem: Cannot paginate the products using limit and offset as offset is only available for the top level relation which here is categories.
Problem: Cannot filter on the basis of category_slug since is not available inside nested
category
I guess because this is a Many-to-one relation.
How can achieve the result I want using the relation queries?7 Replies
I guess you need to query by products
In this case, I will arrive at problem no 2 I have mentioned above. I can set the value for limit and offset but i cannot set filter for category slug.
Then if you want to use relational queries and filter by categories, seems like it's better to make 2 queries instead of 1
Got it, that's what I ended up doing. Thanks!
Hello @sps_storebud , I'm running into same issue you have faced.
Articles-Tags many-to-many, filter articles by tag.Can you please share how you solved it with 2 queries?
Here is what i ended up doing
Sorry i am on phone so couldn't format correctly
Thank you, helped a lot!