laravel query group by sum 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();
Example 3: laravel sum group by
$docs = Document::groupBy('users_editor_id')
->selectRaw('sum(no_of_pages) as sum, users_editor_id')
->get();
@foreach($docs as $doc)
{{$doc->users_editor_id}} >
@endforeach