在 laravel-admin 中,可以使用 groupBy 方法对列表进行分组。

例如,我们有一个 orders 表,其中包含 iduser_idamountcreated_at 四个字段,我们想要按照用户分组统计订单总金额,可以在 Order 模型中添加如下方法:

public static function groupByUser()
{
    return self::query()->selectRaw('user_id, SUM(amount) as total_amount')
        ->groupBy('user_id')
        ->orderByDesc('total_amount');
}

然后在 OrderControllerindex 方法中调用 groupByUser 方法:

public function index(Content $content)
{
    return $content
        ->header('Orders')
        ->description('List')
        ->body(Order::groupByUser()->paginate());
}

这样就可以在列表中按照用户分组展示订单总金额了

laravel-admin 列表group by

原文地址: https://www.cveoy.top/t/topic/ecHs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录