2 回答
TA贡献1875条经验 获得超3个赞
不要将产品和相关产品一起发送到视图中,而不是将产品发送到视图中。
控制器
$categories = Category::with(['products' => function ($query) {
$query->where('status', StatusConstant::PT_ACTIVE);
$query->with(['joindraw' => function ($query) {
$query->where('user_id', $this->user->id);
}]);
}])->get();
return view('store.index', ['categries' => $categries]);
现在,您可以遍历每个类别并访问其相关产品。
刀
<div class="row">
<div class="col s12">
<ul class="tabs">
@foreach($categries as $category)
<li class="tab"><a class="" href="#{{ $category->id }}">{{ $category->name }}</a></li>
@endforeach()
</ul>
</div>
@foreach($categories as $category)
<div id="{{ $category->id }}" class="col s12">
<div class="contents-tabs">
<div class="table-contents default-table">
<!-- your code goes here -->
<!-- Imagine you wanna list product names. -->
<ul>
@foreach($category->products as $product)
<li>{{ $product->name }}</li>
@endforeach()
</ul>
</div>
</div>
</div>
@endforeach()
</div>
TA贡献1790条经验 获得超9个赞
您可以按类别对产品进行分组:
$categoryProducts = [];
foreach($products as $product){
foreach($product->categories as $category){
if(!isset($categoryProducts[$category->id])){
$categoryProducts[$category->id] = [];
}
$categoryProducts[$category->id][$product->id]=$product;
}
}
然后,您可以在每个标签中的类别ID中显示每个类别的产品。
- 2 回答
- 0 关注
- 167 浏览
添加回答
举报