Dewagg
Dewagg

Reputation: 103

Laravel query order by sum

Sorry for my bad English. I have table: recent_posters

I would like to show daily top 10 posters. Something I've tried:

RecentPosters::select('id', 'user', DB::raw('SUM(id) as daily_posts'))
    ->groupBy('id')
    ->orderByRaw('SUM(id) DESC')
    ->limit(10)
    ->get();

But this gives me array with same user, how can I show 10 different users? Thanks in advance.

Upvotes: 2

Views: 128

Answers (2)

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

To get today's top ten posts by different users :

    $today_top_10= RecentPosters::whereRaw('Date(created_at) = CURDATE()')
                                  ->distinct('user')
                                  ->orderBy('created_at','desc')
                                  ->take(10)
                                  ->get();

Upvotes: 1

Do you have a timestamp in your DB. Maybe you can use it to order the posters by date added. And then set a limit of 10.

Upvotes: 0

Related Questions