我有Model Category和products,Model Category有2个关系:
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public function parent()
{
return $this->belongsTo(self::class, 'parent_id')->withoutGlobalScope('active');
}和模型产品有:
public function category()
{
return $this->belongsTo(Category::class);
}我需要分类的groupBy产品,我有代码:
$products = Product::with('category')->whereHas('category', function ($query)
{
$query->where('parent_id', null); //for main categories
});在刀片式服务器I groupBy中:
@forelse($products->groupBy('category.title') as $title => $prods)如何检查父类别中的产品是否为空,并从子类别中写入带有父类别标题的产品?现在我得到的结果是空的..
发布于 2018-12-02 20:47:23
不要在视图中创建groupBy ...试试这个:
$products = Product::with('category')->whereHas('category', function ($query)
{
$query->where('parent_id', null); //for main categories
})
->get()
->groupBy('category.title');此外,您可能必须执行与该查询相反的操作:
$categories = Category::withoutGlobalScope('active')
->where('parent_id',null)
->with('products')
->get();记住将products hasMany关系添加到Category模型中
https://stackoverflow.com/questions/53579113
复制相似问题