Laravel雄辩查询得到结果groupBy(月) [英] Laravel eloquent query to get the result groupBy(month)

查看:1616
本文介绍了Laravel雄辩查询得到结果groupBy(月)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取结果 groupBy(month('start_date'))

到目前为止

    $this->data['earnings'] = DB::table('documents')
        ->leftJoin('users','users.id', '=', 'documents.users_id')
        ->leftJoin('users_editors','users_editors.user_id','=','users_id')
        ->groupBy(month ('start_date'), 'DESC')
        ->sum('amount')
        ->get();

我正在为所有编辑者获得收入 groupBy 月份将从 start_date 中获取一个 groupBy 上。
谢谢

I am trying to get earnings for all the editors groupBy month which will take month from start_date to have a groupBy on. Thanks

推荐答案

为了使用(My)SQL函数,您需要未处理和绑定的原始语句通过PDO或在这种情况下被视为字段名称:

In order to use (My)SQL functions you need raw statements, that are not processed and bound by PDO or, in this case, treated as a field name:

->groupBy(DB::raw('month(start_date)'))






为了使它按照您的期望工作:


In order to make it work as you expect:

->selectRaw('month(start_date) as month, sum(amount) as sum')

->groupBy(DB::raw('month desc'))
// or:
->groupBy('month')
->orderBy('month', 'desc')

->get();

这篇关于Laravel雄辩查询得到结果groupBy(月)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆