laravel - Laravel,尝试使用数据库中的值进行计数
问题描述
我需要一些关于 Laravel 的帮助,我正在尝试使用我的数据库中的信息和计数来做一个列表,例如: 列表图像
如何计算同一地点在我的列表中出现的次数?记住:我的地点列表来自我的数据库,地点是可变的。我数不过来。这是我的代码:
<table border=`1px` class="table-teste">
<tr>
<td><h3> Place</h3></td>
<td><h3> Count</h3></td>
</tr>
@foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
</tr>
@endforeach
</table>
我的控制器:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor')
->get();
解决方案
用户 groupBy 来计算查询中列表中的每个项目,如下所示:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor', , DB::raw('count(*) as total'))
->get();
然后在刀片视图中:
@foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
<td>{{$setor->total}}</td>
</tr>
@endforeach
希望能帮助到你...
推荐阅读
- javascript - 为什么赛普拉斯没有运行我的插件文件?
- android - 尝试在 VideoView Android 中播放视频时出现 FileNotFoundException
- c# - 如果命令被另一个控件触发,有没有办法在“xaml.cs”后面的代码中知道
- three.js - 如何使用着色器在 Three.js 中进行平滑过渡?
- airflow - 如何检查气流调度程序的健康和状态?
- visual-studio - 如何同时测试客户端和服务器应用程序?
- php - 从 WooCommerce 订单表中自定义总行数
- go-templates - Go 模板正在为输出添加额外的引号
- python - 好数字的最大区间范围
- c++ - 总是用 g++ 获得有趣的分段错误