Laravel:平均百分比

时间:2018-01-03 12:34:36

标签: laravel sum average percentage relationships

我从关系表中得到两个数字,我计算这样的百分比:

$safe_voters = AddMember::with('settlement')->whereHas('settlement', function($query) {
  $query->where('reon_id', '1');
  })->where('town_id', Auth::user()->town_id)->count();

$members = AddMember::with('settlement')->whereHas('settlement', function($query) {
  $query->where('reon_id', '1');
  })->where('town_id', Auth::user()->town_id)
  ->where('cipher_id', '0')
  ->count();

$percent_members = round(($members / $safe_voters) * 100,1);

通过这种方式,我将所有safe_voters与reon_id == 1和所有safe_voters以及reon_id == 1和cipher_id == 0一起得到并计算百分比。

但是,我还需要一个变量,它将每个safe_voters和成员的百分比总和分开。例如:

$safe_voters1 = 6;
$member1 = 3;
$percent1 = round(($member1 / $safe_voters1) * 100,1);

$safe_voters2 = 9;
$member2 = 3;
$percent2 = round(($member2 / $safe_voters2) * 100,1);

$safe_voters3 = 12;
$member3 = 3;
$percent3 = round(($member3 / $safe_voters3) * 100,1);

$final_percentage = $percent1  + $percent2 + $percent3;

$ final_percentage在这种情况下应为9号。

如果我没有单独使用每个百分比,如何获得$ final_percentage?

还有一件事,safe_voters和成员的数量正在增加,所以我想我首先需要foreach循环?我想我必须从循环中获得百分比,但我不知道如何......

0 个答案:

没有答案