如何对特定人(id)的元素求和 - laravel
P粉450079266
2023-09-04 16:39:02
<p>您好,我如何对特定人员(id)的表列进行求和。例如,我有一个像这样的表(这是表 ProjectHistory 表): </p>
<p>我想在我的 colabs 视图中显示特定协作者的所有总和,例如查看下图。(这是协作者表)</p>
<p>所以,如果你看一下图片,我就会看到“David”合作的 id 2。所以我想在“Suma”列中输出 ID 为 2 的 colab 的 ProjectHistory 表的总和,因此 ( 3 + 500 = 503 )。我想为所有合作做到这一点。例如,对于 colab_id 3,我想在“Colaboratori”视图中显示(对于 id 3 表示 Valentin)2500+1800 = 4300。我该怎么做?请给我一个想法</p>
You can use the
sum
in the QueryBuilder.像这样:
In your case, you may also use the
withSum
to obtain the result you want. Like so:This resulting Collaborator will have an extra key now named:
projecthistory_sum_suma
which will have the sum of all thesuma
belonging to that Collaborator.This of course assumes that you have a
hasMany
relation with theProjectHistory
model.