PS - I've tried to edit this question to make the formatting better but the little humanity check thing is saying I haven't answered the question and I absolutely have.
Group by month and order by something else?
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:
JULY
Post 1 - score 10 Post 2 - score 8 Post 3 - score 7
JUNE
Post 1 - score 20 Post 2 - score 13 Post 3 - score 7
Really stumped, any help appreciated.
@rodrigo.pedra @jlrdw @marianomoreyra
I solved this really simply within the blade.
I changed
@foreach($groupedposts as $post)
to
@foreach($groupedposts->sortByDesc('post_likes') as $post)
And it's perfect.
Please or to participate in this conversation.