What you mean by most votes? One contestant with most? Or votes in total for the whole office? I would probably use one query to determine the offices and a second to get the data
Eloquent HasManyThrough with Count and Limit
I need help putting this into Eloquent. I have a setup similar to the documentation, https://laravel.com/docs/8.x/eloquent-relationships#has-many-through except my situation is Offices has many Contestants has many Votes.
Offices
====
id
name
Contestants
========
id
office_id
name
email
Votes
====
id
contestant_id
Unfortunately, I'm not on my work laptop so I don't have code to share, but I'm trying to get the top 3 people (most votes, ties broken by date entered - see Paul and George in Office A), grouped by the offices. I'd like to get the count of the votes for contestants and only the top 3 (3 with most votes). The output should look something to the following:
| office | contestant | date_entered | number_votes |
------------------------------------------------
| Office A | John | 2021-11-01 08:00 | 25 |
| Office A | Paul | 2021-11-03 07:48 | 22 |
| Office A | George | 2021-11-03 08:00 | 22 |
| Office B | Ringo | 2021-11-02 08:00 | 18 |
| Office B | Robert | 2021-11-03 08:00 | 17 |
| Office B | Jimmy | 2021-11-03 08:00 | 16 |
| Office C | John | 2021-11-03 08:00 | 26 |
| Office C | John | 2021-11-03 08:00 | 25 |
| Office C | John | 2021-11-03 08:00 | 24 |
I can get this pretty close in SQL (I can't limit it to only 3 per group), but I'd rather have the data retrieved via Eloquent. Can someone point me in the right direction to get this result?
Also, if someone can show me how to format the above as a proper table that would be great also! Thank you.
Please or to participate in this conversation.