laravel group by relationship sum collection code example
Example 1: sql select sum group by id laravel
Document::groupBy('users_editor_id')
->selectRaw('sum(no_of_pages) as sum, users_editor_id')
->pluck('sum','users_editor_id');
array(
users_editor_id => sum,
...
)
Example 2: sql select sum group by id laravel join
$users = User::select('users*', 'analytics.*', DB::raw('SUM(analytics.revenue) As revenue'))
->leftJoin('analytics', 'analytics.user_id', '=', 'users.id')
->where('analytics.date', Carbon::today()->toDateString())
->get();