Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

relation - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Sales::with(['product_detail.product' => function($query){
        $query->groupBy('product_name');
    }])->get();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...