如何使用 Laravel 雄辩获得最大值 + 其他列



这是我的查询:

$maxvalue= market::whereBetween('created_at', [$from, $to])->max('price');

但除了max值之外,我还想获取user_id列。我该怎么做?

您将无法使用max()Eloquent 函数,但您可以将 MySQL 的max()函数与DB::raw()一起使用。您还需要使用 groupBy,以便您可以按用户获取它:

$maxValues = market::whereBetween('created_at', [$from, $to])
->groupBy('user_id')
->get(['user_id', DB::raw('max(price) as max_price'));

最新更新