P粉7901875072023-09-05 10:53:19
You can use the sum
in the QueryBuilder.
像这样:
$data = ProjectHistory::where('id', 1)->sum('suma');
In your case, you may also use the withSum
to 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_suma
which will have the sum of all the suma
belonging to that Collaborator.
This of course assumes that you have a hasMany
relation with the ProjectHistory
model.