【发布时间】:2016-06-30 22:21:53
【问题描述】:
我不擅长编写处理大型数据集的查询。我想尝试一下 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'。
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
好吧,因为它甚至不工作,“更”高效将只是工作
-
{$visitor-total}}语法错误,缺少对象访问器方法的>。另外,您的查询有什么问题?慢吗?它不会产生预期的结果吗? -
你得到这个错误是因为一个错字:{{$visitor-total}} 应该是 {{$visitor->total}}
-
您可能还想阅读
Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel.pagination -
是问题:{{$visitor-total}} -> {{$visitor->total}} ?