【发布时间】:2018-12-02 09:51:07
【问题描述】:
我有模型类别和产品,模型类别有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
});
在刀片中我分组:
@forelse($products->groupBy('category.title') as $title => $prods)
如何检查父类别中的产品是否为空并使用父类别标题编写子类别中的产品?现在我得到空结果..
【问题讨论】: