Laravel Blade上的筛选器枢轴表

我有表格-带有数据透视表的产品和类别。我需要做的是在选项卡中的视图上显示类别并获取相应的产品。


// Product.php

public function categories(){

    return $this->belongsToMany('App\Models\Category');

}


// Category.php

public function products(){

    return $this->belongsToMany('App\Product');

}


// IndexController.php

$products = Product::where('status', StatusConstant::PT_ACTIVE)

                ->with(['joindraw' => function ($query){

                        $query->where('user_id', $this->user->id);

                }])->get();


return view('store.index', ['products' => $products, 'category' => $category]);

我期望这样的输出:


<div class="row">

    <div class="col s12">

        <ul class="tabs">

            <li class="tab"><a class="" href="#tabs1">Category 1</a></li>

            <li class="tab"><a class="active" href="#tabs2">Category 2</a></li>

            <li class="tab"><a class="" href="#tabs3">Category 3</a></li>

        </ul>

    </div>

    <div id="tabs1" class="col s12">

        <div class="contents-tabs">

            <div class="table-contents default-table">

              //Products in category id 1...

我可以知道如何在刀片上做过滤器吗?


倚天杖
浏览 168回答 2
2回答

翻过高山走不出你

不要将产品和相关产品一起发送到视图中,而不是将产品发送到视图中。控制器$categories = Category::with(['products' => function ($query) {&nbsp; &nbsp; $query->where('status', StatusConstant::PT_ACTIVE);&nbsp; &nbsp; $query->with(['joindraw' => function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; $query->where('user_id', $this->user->id);&nbsp; &nbsp; }]);}])->get();return view('store.index', ['categries' => $categries]);现在,您可以遍历每个类别并访问其相关产品。刀<div class="row">&nbsp; &nbsp; <div class="col s12">&nbsp; &nbsp; &nbsp; &nbsp; <ul class="tabs">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($categries as $category)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="tab"><a class="" href="#{{ $category->id }}">{{ $category->name }}</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach()&nbsp; &nbsp; &nbsp; &nbsp;</ul>&nbsp; &nbsp; </div>&nbsp; &nbsp; @foreach($categories as $category)&nbsp; &nbsp; &nbsp; &nbsp; <div id="{{ $category->id }}" class="col s12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="contents-tabs">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="table-contents default-table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- your code goes here -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Imagine you wanna list product names. -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($category->products as $product)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>{{ $product->name }}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; @endforeach()</div>

富国沪深

您可以按类别对产品进行分组:$categoryProducts = [];foreach($products as $product){&nbsp; &nbsp; foreach($product->categories as $category){&nbsp; &nbsp; &nbsp; &nbsp; if(!isset($categoryProducts[$category->id])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $categoryProducts[$category->id] = [];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $categoryProducts[$category->id][$product->id]=$product;&nbsp; &nbsp; }}然后,您可以在每个标签中的类别ID中显示每个类别的产品。
打开App,查看更多内容
随时随地看视频慕课网APP