【问题标题】:Pagination shows error when use having in query builder在查询生成器中使用时分页显示错误
【发布时间】:2018-12-05 21:45:00
【问题描述】:

我有一个如下查询,它按预期工作。我在这里使用了havingRaw 选项来过滤结果。

$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as total_orders"))
    ->groupBy('mobile')
    ->havingRaw('total_orders > 12')
    ->orderBy('total_orders', 'desc')
    ->get();

由于返回行的总数会有所不同,我需要将其显示为分页。所以我改变了如下查询。然后它显示找不到total_orders 列的错误。

$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as total_orders"))
    ->groupBy('mobile')
    ->havingRaw('total_orders > 12')
    ->orderBy('total_orders', 'desc')
    ->paginate();

我为以前版本的 Laravel 找到了一些解决方法。我的项目使用 Laravel 5.6,有什么解决方案吗?

【问题讨论】:

标签: laravel eloquent


【解决方案1】:

试试这个..

$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as 
  total_orders"))
 ->groupBy('mobile')
 ->having('total_orders','>', 12)
 ->orderBy('total_orders', 'desc')->get();

【讨论】:

  • 我更新了问题,我遇到了paginate()的问题
  • column not found error getting in orderBy 或 Have ?
  • having 适用于 get() 但不适用于 paginate()
猜你喜欢
  • 2014-05-30
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多