数据库多级分类
数据库模型Building表名buildings
id、parent_id、level、title、area | |
1 0 0 B1楼 NULL | |
2 1 1 一层 NULL | |
3 2 2 101室 105 | |
4 2 2 102室 102 | |
5 2 2 103室 120 | |
6 2 1 二层 NULL | |
7 6 2 201室 120 | |
8 6 2 202室 100 |
多级分类children
public function childCategory() | |
{ | |
return $this->hasMany('App\Models\Building', 'parent_id', 'id'); | |
} | |
public function children() | |
{ | |
return $this->childCategory()->with('children')->orderBy('sort', 'asc'); | |
} |
如何像children一样把每级的房间面积数汇总area算出来啊?我用了resource 只算出第一级的面积,里面的children如何计算,能否指导下啊?