我在编写处理大型数据集的查询时非常糟糕。我想刺激Laravel看看我能想出什么。有没有更好的方法来编写此查询并输出结果?
代码
$visitors = DB::table('visitors')
->where('id_client',1)
->select('id', 'ip', DB::raw('count(*) as total'), 'date', 'city', 'region')
->groupBy('ip')
->orderBy('date', 'DESC')
->simplePaginate(25);
结果
@foreach($visitors as $visitor)
<tr>
<td>
{{$visitor->id}}
</td>
<td>
@if (date('F d, Y', strtotime($visitor->date)) === date('F d, Y'))
Today
@else
{{ date('F d, Y', strtotime($visitor->date)) }}
@endif
</td>
<td>
{{$visitor-total}}
</td>
<td>
{{$visitor->city}}
</td>
<td>
{{$visitor->region}}
</td>
</tr>
@endforeach
{!! $visitors->render() !!}
此外,我在尝试加载页面时收到错误Use of undefined constant total - assumed 'total'
。
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
只要满足您的需要,查询就会很好。
如果您的查询运行速度太慢,也许您可以在“id_client”列中添加一个索引,以便更快地进行搜索(如果您当前没有这样做)。