簡體   English   中英

從laravel獲取所有類別級別的產品

[英]getting products from all category levels in laravel

假設我的類別級別如下:

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

現在我正在訪問Parent頁面。 但我想從各個層面獲得所有產品,而不僅僅是父母。

  1. 我如何獲得parent child one child twochild two(one)
  2. 如何在parent頁面上通過分頁顯示它們?

代號

這是我目前擁有的東西,它僅顯示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)

任何想法?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM