如何计算刀片模板的平均值? [英] how to calculate average in blade template?

查看:83
本文介绍了如何计算刀片模板的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有此图片

是否可以计算内部刀片模板的平均值?我正在使用foreach循环显示学生的学科和成绩,并且在平均部分上如何求平均,在此问题上通过刀片或控制器计算的最佳方法是什么?目的是产生总平均数.

Is it possible to calculate the average inside blade template? I'm using foreach loop to show the subject and grade of the student and on the average section how can I total average, what is the best approach on this matter calculate through blade or controller? aim is to generate total avarage.

这是查看代码:

@foreach($scores as $score)

<td>&nbsp;{{$score->subject->subject_name}}</td>
<td>&nbsp;{{$score->result}}</td>
<td>&nbsp;{{$score->getGrade()}}</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;{{$score->final}}</td>
<td>&nbsp;{{$score->average()}}</td>

@endforeach

推荐答案

避免混淆的最好方法是在控制器中计算平均值并将其作为变量发送,因此您不必在视图中操作数据.您可以通过两种方式做到这一点,在视图中手动进行或在控制器中使用模型.

The best way to avoid confusion is to calculate the average in the controller and send it as a variable, so you don't have to manipulate data in the view. You can do it both ways, manually in the view or using the model in the controller.

以下是使用模型的示例:如何获取laravel中列值的平均值

Here is an example of using the model: How to get average of column values in laravel

$average = Scores::avg('average')

或者在视图中手动执行此操作,方法是将以前的值添加到变量中并用count($scores)除.

Or do it in the view manually by adding previous values to a variable and dividing with count($scores).

希望有帮助.

这篇关于如何计算刀片模板的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆