Laravel Query Buider Group By Not Getting All The Records

扶醉桌前 提交于 2019-12-24 11:37:15

问题


I am trying to get the data using groupBy on type field from my transaction table. I am using this query

DB::table($this->table)
    ->select()
    ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
    ->groupBy('type')
    ->get();

But it is not giving the complete records. There are more than 10 records in my table. But it is giving me only two. One for type=1 and another for type=2. It is selecting only on record from each type. I am expecting that i will get all the transactions based on condition grouped in two result set. Anyone know why it is happening?


回答1:


Try to call Collection groupBy instead. Just put groupBy after get(). It should work.

DB::table($this->table)
    ->select()
    ->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
    ->get()
    ->groupBy('type');



回答2:


Faq::where('type','host')->get()->groupBy('category');



来源:https://stackoverflow.com/questions/48225791/laravel-query-buider-group-by-not-getting-all-the-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!