I have a problem with displaying the list of products on subpages of product categories.
I created two models: Products and Categories.
Products model use $belongsTo
(categories) relation.
Categories model use \October\Rain\Database\Traits\NestedTree
to create nested categories and $hasMany
(products) relation.
URL structure for category pages: "/products/:slug?/:sub?"
In component I use that code:
if ($this->param('sub')) {
// Child category - that works
$query = productsModel::orderBy($order, $orderDirection)->get();
$currentcategory = categoriesModel::where('slug', $this->param('sub'))->first();
$query = $query->where('categories_id', $currentcategory->id);
} elseif ($this->param('slug')) {
// Parent category / Category without subcategories
// How to get all products whose parent category slug is equal $this->param('slug')?
}
I’ve tried various things but nothing works. I will be grateful for help