Laravel Query Buider Group By Not Getting All The

2019-07-23 06:08发布

问题:

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');