I'm creating a rest api with Lumen. I have pictures
& picture_likes
tables & models and I am trying to get most liked pictures.
My Pictures table
looks like..
photo_id | path | owner_id | created_at
My PictureLikes table
looks like...
id | user_id who liked
0 1
1 2
2 4
3 3
4 5
etc...
My first question is how to get most liked pictures. I think I can achieve most liked photo by grouping similar photo_ids and ordering them by their counts. However, I couldn't figure out how to order by count.
And then, I can go even further and get most liked photos of last 7 days (which will move relative to today). My second question is, how can I filter the most liked pictures, only to display most liked photos of last X days?
Update: I succeed on my first question with this:
$photos = Photo::all(); // returns all photos
$photoLikes = PhotoLikes::all(); // returns all photoLikes
$photoLikes = PhotoLike::select('photo_id', \DB::raw('count(*) as total'))
->groupBy('photo_id')
->orderBy('total', 'desc')
->get()->toArray();
The result looks:
array:10 [
0 => array:2 [
"photo_id" => 1
"total" => 5
]
1 => array:2 [
"photo_id" => 10
"total" => 5
]
..etc
However, how can I use this variable to get the photos from just last X days?