我正在为类别和子类别创建嵌套导航。我在BaseController
中有一个构造函数,它与视图共享一个变量,返回类别模型中的所有内容。
基本控制器
public function __construct() {
$this->beforeFilter(function() {
View::share('catnav', Category::all());
});
}
到目前为止,我可以显示类别的名称和子类别的对象数组,如下所示:
@foreach($catnav as $cat)
<li>{{ HTML::link('/store/category/'.$cat->id, $cat->name) }}
<ul>
<li>{{ HTML::link('/store/category/'.$cat->id, $cat->subcategories) }}</li>
</ul>
</li>
@endforeach
以上输出为:
- Category Name
- [{"id":6,"category_id":2,"name":"Electric","created_at":"2014-11-14 15:13:23","updated_at":"2014-11-14 15:13:23"},{"id":7,"category_id":2,"name":"Acoustic","created_at":"2014-11-14 15:13:26","updated_at":"2014-11-14 15:13:26"}]
- Another Category Name
问题:
我已经查看了Access nested array within laravel views,Nested sets laravel和Sub-categories display under Main categories
答案 0 :(得分:2)
如果数量有限,您可以添加另一个foreach
循环
@foreach($catnav as $cat)
<li>{{ HTML::link('/store/category/'.$cat->id, $cat->name) }}
<ul>
@foreach($cat->subcategories as $subcat)
<li>{{ HTML::link('/store/category/'.$cat->id.'/sub/'.$subcat->id, $subcat->name) }}</li>
@endforeach
</ul>
</li>
@endforeach
对于我刚猜到的实际网址,但我相信您可以自行调整以满足您的需求 另请注意,您可能希望首先检查它是否具有子类别。如果您需要任何帮助,请告诉我。