Search code examples
laravelquery-builderlaravel-5.7laravel-query-builderskip-take

Laravel query builder GROUP BY method alongside with SKIP and TAKE methods


I have around 50000 records and I am showing them in Datatable with server side processing. In my query I am applying the groupBy() method with skip() and take() method.

I want to be able to apply the limit AFTER groupBy() e.g.

If limit is 10 it should return 10 groups not 10 records.

DB::table('actions')->whereBetween('timestamp', 
array($dates['startDate'], $dates['endDate']))
->where('shop_name', $shopName)
->skip($start)
->take(10)
->get()
->groupBy('product_id');

With this query i am getting 10 records not 10 groups.


Solution

  • DB::table('actions')
                ->whereBetween('timestamp', array($dates['startDate'], $dates['endDate']))
                ->where('shop_name', $shopName)
                ->get()
                ->groupBy('product_id')
                ->splice($start,$rowperpage);
    

    and this order worked.