【发布时间】:2018-02-27 08:35:54
【问题描述】:
我希望有人可以帮助我解决我的问题。我对 Laravel 5 的这个小关系问题很感兴趣。
问题:
我想在“钱包”模型中获取我的“金额”列的总和。
我当前正在运行的代码。
public function children()
{
return $this->hasMany('App\Tree', 'parent_id','id')
->with('children')
->with('user')
->with('user.wallets');
}
注意:此返回带有以下数据。
[{
...,
children: [...]
user: {
...,
wallets: [...] // return list of array
}
}]
我的解决方案,但没有运气。
return $this->hasMany('App\Tree', 'parent_id','id')
->with('children')
->with('user')
->with('user.wallets', function($q) {
$q->sum('amount');
});
想要的结果
[{
...,
children: [...]
user: {
...,
wallets: [
...,
amount: XXX.XX // e.g The total sum of the column, could be any number based on the sum of the column.
]
}
}]
【问题讨论】:
-
您还在寻找答案吗?
-
你好@JonasStaudenmeir 是的,你知道这个问题的任何解决方案吗?
标签: php mysql laravel eloquent