【问题标题】:Filter pivot table on Laravel BladeLaravel Blade 上的过滤器数据透视表
【发布时间】:2019-09-17 03:30:56
【问题描述】:

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

// 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...

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您可以按类别对产品进行分组:

    $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】:

      考虑将具有相关产品的类别发送到视图,而不是将产品发送到视图。

      控制器

      $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>
      

      【讨论】:

      • 感谢您的及时回复。尝试使用您的解决方案。我收到语法错误 - 语法错误,意外的 '$query' (T_VARIABLE)。有什么想法吗?
      • 是的,我更新了答案。看一看。提供 clouser 时,with() 函数参数应位于数组中。
      • 相同 :( 我已经尝试过我的编辑。也不起作用。
      • 很抱歉出现语法错误。我想现在一切都解决了。
      猜你喜欢
      • 2014-07-22
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2014-01-12
      • 2020-05-08
      相关资源
      最近更新 更多