[英]getting products from all category levels in laravel
假設我的類別級別如下:
Parent
- child one
- child two
- child two (one)
現在我正在訪問Parent
頁面。 但我想從各個層面獲得所有產品,而不僅僅是父母。
parent
child one
child two
和child two(one)
? 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.