首页 > 解决方案 > Laravel - 如何在 Eloquent Top 5 查询中添加 where 子句

问题描述

我正在使用 Laravel-5.8 Eloquent Query,如下所示:

   $destination = Trip::select('destination')
      ->selectRaw('COUNT(*) AS count')
      ->groupBy('destination')
      ->orderByDesc('count')
      ->limit(5)
      ->get();

如何将其包含在上面的查询中?

其中client_id = $userClientId

标签: laravel

解决方案


您可以直接使用wherebefore group by 或 after。

$destination = Trip::select('destination')
      ->selectRaw('COUNT(*) AS count')
      ->where('client_id', $userClientId)
      ->groupBy('destination')
      ->orderByDesc('count')
      ->limit(5)
      ->get();

推荐阅读