Laravel Grouping by Eloquent Relationship

北城余情 提交于 2019-11-29 06:41:12
Imtiaz Pabel

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

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

this will help you to do grouping by relation.

$sales = Order::Sales('product')
        ->where('approved','=','Yes')
        ->groupBy('product_id')
        ->orderBy(DB::raw('COUNT(id)','desc'))
        ->get(array(DB::raw('COUNT(id) as totalsales'),'product_id'));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!