从laravel中的所有类别级别获取产品

时间:2018-05-10 14:49:23

标签: php laravel

假设我的类别级别如下:

Parent
 - child one
 - child two
   - child two (one)

现在我正在访问Parent页面。但我希望得到所有级别的所有产品,而不仅仅是父级产品。

问题

  1. 如何在parent child one child two& child two(one)
  2. 如何通过parent页面中的分页显示它们?
  3. 这是我目前所拥有的,它只显示parent

    下的产品
     public function totalcategoriessubs($catslug) {
        $categories = Category::where('slug','=',$catslug)->with('childs')->paginate(12);
        $products = Product::whereHas('category', function($q) use($catslug){
          $q->where('slug',$catslug);
          })->paginate(12);
    return view('front.categoriessubs', compact('products', 'categories'));
      }
    

    更新

    我已更改了我的功能并添加了$category,所以现在就像:

    public function totalcategoriessubs($catslug) {
        //changed
        $category = Category::where('slug','=',$catslug)->with('childs')->first();
    
        //testing this
        $products = Product::whereHas('category', function($q) use ($catslug,$category)
        {
          $q->where(function($q) use ($catslug,$category) {
            $q->where('slug',$catslug)->orWhere('category_id',$category->id);
          });
        })->orderBy('created_at', 'DESC')->paginate(10);
        //end testing
    
    return view('front.categoriessubs', compact('products', 'category'));
    

    }

    有了这个,我可以得到product

    列表
    Parent
     - child one
     - child two
    

    但仍然无法获得child two (one)

    的产品

    任何想法?

1 个答案:

答案 0 :(得分:0)

 Category::with(['childsOne', 'childsTwo' => function($query){
    $query->with('ChildsTwoChilds');
    ])->where('slug', $catSlug)->paginate(12)