假设我的类别级别如下:
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)
任何想法?
答案 0 :(得分:0)
Category::with(['childsOne', 'childsTwo' => function($query){
$query->with('ChildsTwoChilds');
])->where('slug', $catSlug)->paginate(12)