多级分类,如何像children一样把每级的面积数area算出来啊?

PHP技术
328
0
0
2022-10-07

数据库多级分类

数据库模型Building表名buildings

id、parent_id、level、title、area
1       0        0     B1楼   NULL
2       1        1     一层   NULL
3       2        2     101105
4       2        2     102102
5       2        2     103120
6       2        1     二层   NULL
7       6        2     201120
8       6        2     202100

多级分类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如何计算,能否指导下啊?