首页 > 解决方案 > PHP 和 Laravel | 在 GroupBy 之后使用 OrderBy

问题描述

这是我的 PHP 代码

 $elements= DB::table('transactions')
  ->select('product_id', DB::raw('count(*) as total'))
  ->groupBy('product_id')
  ->get();

我想应用 orderby 和 limit('5')count

有什么方向吗?谢谢你。

标签: phplaravel

解决方案


添加 orderBy() ..和 take()

 $elements= DB::table('transactions')
  ->select('product_id', DB::raw('count(*) as total'))
  ->groupBy('product_id')
  ->orderBy('your_column')
  ->take(5)
  ->get();

推荐阅读