@deron
To display subcategories in OctoberCMS, you can follow these steps:
1 2 3 |
public $hasMany = [ 'subcategories' => 'NamespaceToSubcategoryModel', ]; |
1 2 3 4 5 |
public function index() { $categories = Category::with('subcategories')->get(); return $this->renderPartial('index', ['categories' => $categories]); } |
1 2 3 4 5 6 7 8 |
{% for category in categories %} <h2>{{ category.name }}</h2> <ul> {% for subcategory in category.subcategories %} <li>{{ subcategory.name }}</li> {% endfor %} </ul> {% endfor %} |
That's it! Now, when you visit the appropriate page using this layout or view, the subcategories will be displayed under their respective categories.