Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Grouping by Eloquent Relationship

How can I group by relation?

Example

Sales::with('product_detail.product')->groupBy('product_name')->get()

How can I get a result with eloquent code?

like image 562
yudijohn Avatar asked Apr 07 '16 06:04

yudijohn


1 Answers

You can specify a callback function for grouping your relation like this:

Sales::with(['product_detail.product' => function($query){
        $query->groupBy('product_name');
    }])->get();
like image 61
Imtiaz Pabel Avatar answered Oct 09 '22 18:10

Imtiaz Pabel