Categories
id | title | parent_id
Products
id | title | category_id
The categories table self joins to make sub categories:
id | title | parent_id
1 art 0
2 sport 0
3 brushes 1
4 paints 1
5 balls 2
Products then belong to a category, so a football product will belong to the balls category which is a sub category of sport.
I have a product and category model.
On the category model I have a hasManyThrough:
public function products()
{
return $this->hasManyThrough('Product', 'Category', 'parent_id', 'category_id');
}
I call this via:
Category::with('products')->find($catId);
This all works, I can get all products within a category, the issue arrises when I need to get all products in a sub category, so with the data:
id | title | category_id
1 blue paint 4
2 red paint 4
3 fine brush 3
And calling:
Category::with('products')->find(1); //get all art products
I get blue paint, red paint and fine brush.
The problem occurs when I call a sub category:
Category::with('products')->find(4); //get all paints
I get nothing, I should get blue and red paint.
How can I call I solve this so that I can get all products for a parent or sub category?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire