Php 从laravel的所有类别级别获取产品
假设我的类别级别如下:Php 从laravel的所有类别级别获取产品,php,laravel,Php,Laravel,假设我的类别级别如下: Parent - child one - child two - child two (one) 现在我正在访问家长页面。但我想从各个层面获得所有产品,而不仅仅是家长 问题: 我如何才能访问父级子级子级中的产品 两个&子二(一) 如何在parent页面中分页显示它们 代码 这是我目前拥有的,它仅显示父项下的产品 public function totalcategoriessubs($catslug) { $categories = Category
Parent
- child one
- child two
- child two (one)
现在我正在访问家长
页面。但我想从各个层面获得所有产品,而不仅仅是家长
问题:
子级
子级中的产品
两个
&子二(一)
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'));
}
有了这个,我可以得到产品
产品列表
Parent
- child one
- child two
但仍然无法获得儿童二(一)
你知道吗?你的分类树有多深?@vieny我没有对它进行限制,但我想在
儿童二(一)
之前需要的最大值是2。你试过('childs.childs')吗?@JonasStaudenmeir无人应该修复的部分是$products=Product:…
在这里,我必须包括$CATEGORES
儿童的产品,所以它们都是……。知道吗?不,兄弟,你弄错了,不是关于类别名称,而是关于深度。类别名称是动态的,可能是我无法硬编码的任何内容。
Category::with(['childsOne', 'childsTwo' => function($query){
$query->with('ChildsTwoChilds');
])->where('slug', $catSlug)->paginate(12)