在 laravel-8 中找到 $BatterRuns 变量的总和

find sum of $BatterRuns variable in laravel-8

我想求 $BatterRuns 变量的总和,该怎么做? 注意;_我只是laravel的初学者 这是我的 index.blade.php

@foreach ($data as $key => $row)
  <tr>
@php
        $BatterRuns = $row->scoreupdateone->one + $row->scoreupdatetwo->two * 2 +
        $row->scoreupdatethree->three * 3 + $row->scoreupdatefour->four * 4 +
        $row->scoreupdatesix->six * 6;
@endphp
         <td>{{ $key + 1 }}</td>
         <td>{{ $row->matchh->match_name }}</td>
         <td>{{ $row->team->team_name }}</td>
         <td>{{ $row->player->player_name }}</td>
         <td>{{ $BatterRuns }}</td>
         <td>{{ $row->scoreupdate->out_type }}</td>
         <td>{{ $row->scoreupdateoutbytype->out_by_type }}</td>
         <td>{{ $row->scoreupdateone->one }}</td>
         <td>{{ $row->scoreupdatetwo->two }}</td>
         <td>{{ $row->scoreupdatethree->three }}</td>
         <td>{{ $row->scoreupdatefour->four }}</td>
         <td>{{ $row->scoreupdatesix->six }}</td>
         <td>{{ $row->scoreupdateballsplayed->balls_played }}</td>
@php
         $Balls = $row->scoreupdateballsplayed->balls_played;
@endphp
                                                
@endforeach

这就是我得到的,你可以看到我从 $BatterRuns 变量中得到的运行列。

您可以看到 Runs 列中有不同的值,例如 4,6,0 我想要 sam 值 4+6+0 = 10。我想要 $sum = SUM($BatterRuns);如何做到这一点

您可以在您的代码中执行类似的操作。

@php
 $totalRun = 0;
@endphp
@foreach ($data as $key => $row)
  <tr>
@php
        $BatterRuns = $row->scoreupdateone->one + $row->scoreupdatetwo->two * 2 +
        $row->scoreupdatethree->three * 3 + $row->scoreupdatefour->four * 4 +
        $row->scoreupdatesix->six * 6;
        $totalRun = $totalRun + $BattersRuns;

@endphp
         <td>{{ $key + 1 }}</td>
         <td>{{ $row->matchh->match_name }}</td>
         <td>{{ $row->team->team_name }}</td>
         <td>{{ $row->player->player_name }}</td>
         <td>{{ $BatterRuns }}</td>
         <td>{{ $row->scoreupdate->out_type }}</td>
         <td>{{ $row->scoreupdateoutbytype->out_by_type }}</td>
         <td>{{ $row->scoreupdateone->one }}</td>
         <td>{{ $row->scoreupdatetwo->two }}</td>
         <td>{{ $row->scoreupdatethree->three }}</td>
         <td>{{ $row->scoreupdatefour->four }}</td>
         <td>{{ $row->scoreupdatesix->six }}</td>
         <td>{{ $row->scoreupdateballsplayed->balls_played }}</td>
@php
         $Balls = $row->scoreupdateballsplayed->balls_played;
@endphp
                                                
@endforeach
</tr>
<tr>
 <td>{{ $totalRuns }}</td>
</tr>

在 for-loop 之前声明一个 php 变量,并在 for-loop 中使用相同的变量来总结运行。并使用变量 $totalRuns 显示 $battersRun

的总和