您好,我如何对特定人员(id)的表列进行求和。例如,我有一个像这样的表(这是表 ProjectHistory 表):
我想在我的 colabs 视图中显示特定协作者的所有总和,例如查看下图。(这是协作者表)
所以,如果你看一下图片,我就会看到“David”合作的 id 2。所以我想在“Suma”列中输出 ID 为 2 的 colab 的 ProjectHistory 表的总和,因此 ( 3 + 500 = 503 )。我想为所有合作做到这一点。例如,对于 colab_id 3,我想在“Colaboratori”视图中显示(对于 id 3 表示 Valentin)2500+1800 = 4300。我该怎么做?请给我一个想法
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
You can use the
sumin the QueryBuilder.像这样:
$data = ProjectHistory::where('id', 1)->sum('suma');In your case, you may also use the
withSumto obtain the result you want. Like so:$data = Collaborator::withSum('ProjectHistory', 'suma')->get();This resulting Collaborator will have an extra key now named:
projecthistory_sum_sumawhich will have the sum of all thesumabelonging to that Collaborator.This of course assumes that you have a
hasManyrelation with theProjectHistorymodel.