0

I know that Laravel can do simple math and stuff... but I just confused if the data is from the same column... how to SUM that up?

The table:

enter image description here

sistem.blade.php:

<table border='1'>
<th>Umur</th><th>Laki-laki</th><th>Perempuan</th><th>Jumlah</th>
@foreach($age as $a)
<tr>
<td>{{ $a->range }}</td><td>{{ $a->lk }}</td><td>{{ $a->pr }}</td><td>{{ $a->lk+$a->pr }}</td>
</tr>
@endforeach
<tr>
<td>Jumlah</td><td>sum lk</td><td>sum pr</td><td>sum lk+pr</td>
</tr>
</table>

I was wondering how am I supposed to code SUM lk, pr, and lk+pr, is it in controller or can it be done in resource/view blade file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TheBetweener
  • 33
  • 10
  • Does this answer your question? [How can I sum objects property of an array using PHP](https://stackoverflow.com/questions/30726537/how-can-i-sum-objects-property-of-an-array-using-php) – WebPajooh Aug 01 '22 at 17:41
  • Oh well, it turns out using $sum = model::sum('sum_field'); and $sum = model::where('index', 'search')->sum('sum_field'); is simpler. – TheBetweener Aug 03 '22 at 10:09

1 Answers1

0

You can do it in both places but the most visual thing for the programmer is to do it in the view. However, a good practice would be to have the view as clean as possible, so it's better to create your own object in the controller to iterate through the view.

abel-gg
  • 1
  • 2