I have the following posts:
title rating created_at
-----------------------------------
Post title 1 3 2021-06-21 13:00:19
Post title 2 5 2021-06-16 13:00:19
Post title 3 4 2021-03-21 13:00:19
Post title 4 7 2021-03-18 13:00:19
Post title 5 10 2021-02-26 13:00:19
I want to list the latest 3 best rated posts within a month, but there are only 2 posts, so I want to add 1 more. I want to get this result:
Post title 2 - rating 5
Post title 3 - rating 4
Post title 1 - rating 3
Is it possible in Laravel? I use the following code, but it only lists two posts:
Post::query()->orderBy('rating', 'desc')
->where('created_at', '>=', Carbon::now()->subDays(30))
->limit(3)
->get();
source https://stackoverflow.com/questions/68141384/how-to-get-the-latest-posts-with-a-condition-within-a-month-in-laravel
Comments
Post a Comment