简体   繁体   中英

getting products from all category levels in laravel

Let say my category level is like:

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

Now I am visiting Parent page. but i want to get all products from all levels and not only parent.

Question

  1. How can I have access to products in parent child one child two & child two(one) ?
  2. How to show them by pagination in parent page?

Codes

this is what i have currently and it only shows products under 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'));
  }

UPDATE

I have changed my function and added $category to it so now is like:

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'));

}

with this i can get product list of

Parent
 - child one
 - child two

but still can't get products of child two (one)

any idea?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM