我使用这个查询来获取每个类别的子项
$this['children'] = Cat::first()->children;但它只获取第一个类别的子类别,而所有其他类别都显示相同的子类别。对如何解决这个问题有什么想法吗?
模型关系
public function children(){
return $this->hasMany(static::class,'parent_id','id');表结构
类别表
id
cat_title
parent_id
nest_right
nest_left
nest_depth
弹状物
parent_id =0(类别)
parent_id >0(子类别)
发布于 2019-02-25 08:01:45
要获得每个类别的子类别,首先需要获取每个类别的id,并在查询中使用它。(例如,在foreach循环中)
$this['children'] = Cat::find($id)->children;如果您需要同时获取所有类别及其相关子类别,则需要立即加载:
$allCatsWithTheirChildren = Cat::with('children')->all();https://stackoverflow.com/questions/54855662
复制相似问题