使用 Laravel Blade 通过分组创建 Table

Creating a Table with Grouping using Laravel Blade

我在数据库中有以下数据

salesperson_id   | item      |   value  
1                   112         100
1                   199         100
2                   155         50

这是存储在一个数组中

[  
  {  
  "salesperson_id":"1",
  "ITEM":"112",
  "TOTAL":100
  },
{  
  "salesperson_id":"1",
  "ITEM":"199",
  "TOTAL":100 
},
{  
  "salesperson_id":"2",
  "ITEM":"155",
  "TOTAL":50
}
]

使用 laravel blade 我如何让数据显示类似于下面的内容

|Sales Person - 1 |
                  | ITEM   |  value |
                    112       100
                    199       100
                                      total £200
 | Sales Person 2 | 
                  | ITEM   |  value |
                    155         50    
                                     total £50

我可以将每一行打印到 table 行,但我想将其更改为上面的分组,如果有人能指出正确的方向,那就太好了,我不是在寻找要为我完成的代码。

这是 Collection::groupBy

的一个很好的用例
<table>
    @foreach($data->groupBy('salesperson_id') as $salesperson)
        <tr>
            <td rowspan="{{ count($salesperson)+1 }}">{{ $salesperson[0]['salesperson_id] }}</td>
            @foreach($salesperson as $item)
                <td>{{ $item['ITEM'] }}</td>
                <td>{{ $item['TOTAL'] }}</td>
            @endforeach
        </tr>
        <tr>
            <td colspan="2">Total: £ {{ $salesperson->sum('TOTAL') }}</td>
        </tr>
    @endforeach
</table>

只需确保 $data 作为集合而不是普通数组传递给您的视图。