1 month ago

Group by month and order by something else?

Posted 1 month ago by Mick79

I'm stuck...

I have a series of posts that can be upvoted by my users. I require these posts to be grouped by month (as this is how I display them on the site) but within the month groups be ordered by "rank".

I thought I had it working with this:

$posts = Post::query()
            ->orderBy('post_likes', 'DESC')
            ->get()->groupBy(function ($date) {
                return Carbon::parse($date->created_at)->format('F');

The above DOES allow me to display the posts by month but if a post in June has a higher score than a post in July then it goes to the top of the list.

This is what I need:


Post 1 - score 10 Post 2 - score 8 Post 3 - score 7


Post 1 - score 20 Post 2 - score 13 Post 3 - score 7

Really stumped, any help appreciated.

Please sign in or create an account to participate in this conversation.