php - 在 Blade Laravel 5.3 中显示聚合

标签 php mysql laravel laravel-blade

我在数据库中有下表 enter image description here

我有以下查询

      $users = DB::table('results')
            ->select('student_id', DB::raw('SUM(obtained_marks) as total_marks'))
            ->groupBy('student_id')
            ->get();

我对这个查询的输出如下,这是根据我的需要,但我很难在我的 Blade 中显示数据 enter image description here

在我看来,当我使用 foreach 循环显示总和结果时,它被打印在一个数组中。下面给出了for循环

     @foreach($users as $names)
     <td>{{$names->studnent_id}}</td>
     <td>{{$names->total_marks}}</td>
     @endforeach

当我执行此操作时,会出现以下 View Current Output

期望输出

student_id ------------------获得的分数

1--------------------------------30

2 ------------------------------70

3--------------------------------60

最佳答案

伙计,你缺少行。

@foreach($users as $names)
    <tr>
        <td>{{$names->student_id}}</td>
        <td>{{$names->total_marks}}</td>
    </tr>
@endforeach

关于php - 在 Blade Laravel 5.3 中显示聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014324/

相关文章:

php - 使用 Sprockets 作为 PHP 应用程序的独立服务

php - 从 XML 向 MySQL 插入数据

php - 电子表格 excel writer 9.2

mysql - SQL 性能 UNION 与 OR

php - 无法使用 Laravel 5 加载 magento 类

php - Laravel 查询生成器使用 MySQL 中的变量进行复杂选择?

php - 每 5 分钟执行一次 PHP cron,但将函数置于 sleep 状态以在 15 分钟后运行

php - 将单独的月、日和年值转换为时间戳

laravel - 如果 token 作为查询字符串出现在 URL 中,则阻止 Laravel API 处理 token

php - 如何实现可以处理用户输入的服务器端应用程序?