Laravel雄辩:与groupBy合计 [英] Laravel Eloquent: sum with groupBy

查看:109
本文介绍了Laravel雄辩:与groupBy合计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我已经尝试过:

  $ this-> data ['no_of_pages'] = Document :: sum('no_of_pages')
- > groupBy('users_editor_id');

哪一个会话给我调用非对象的成员函数groupBy()因为sum()已经执行了查询,并且在应用grouBy()的时候已经准备好了)。所以有人可以指导我吗?

解决方案

  Document :: groupBy('users_editor_id' b $ b  - > selectRaw('sum(no_of_pages)as sum,users_editor_id')
- > pluck('sum','users_editor_id');

//原来的列表(),它被弃用为有利于5.2
//中的摘要,并完全在5.3
// - > lists('sum')中完全删除, 'users_editor_id');


//返回数组,如下所示:
array(
users_editor_id => sum,
...

或者这样(我不会使用,因为它不会是实际的ORM结果):

  Document :: groupBy('users_editor_id')
- > selectRaw('*,sum(no_of_pages)as sum' )
- > get();

//返回带有额外和字段的Document伪模型的集合


Need eloquent/fluent query to get sum with groupBy function.

So far I have tried:

    $this->data['no_of_pages'] = Document::sum('no_of_pages')
                                ->groupBy('users_editor_id');

Which ofcourse gives me call to member function groupBy() on non-object because of the fact that sum() will already execute the query and have result ready by the time grouBy() is applied. So can anyone guide me?

解决方案

Document::groupBy('users_editor_id')
   ->selectRaw('sum(no_of_pages) as sum, users_editor_id')
   ->pluck('sum','users_editor_id');

   // originally lists(), which was deprecated in favour of pluck in 5.2
   // and dropped completely in 5.3
   // ->lists('sum','users_editor_id');


// returns array like this:
array(
  users_editor_id => sum,
  ...
)

Or this way (which I wouldn't use, since it won't be actual ORM result):

Document::groupBy('users_editor_id')
   ->selectRaw('*, sum(no_of_pages) as sum')
   ->get();

// returns collection of Document pseudo models with additional sum field

这篇关于Laravel雄辩:与groupBy合计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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