Setup is very simple. I have an events table with a couple of thousand events (and growing) that took place across the world. These all have a venue, city and country_code column which are self explanatory in contents. They're displayed in an index and then there is the show method for each event. All standard stuff.
On each individual event (eg example.com/events/56 which took place at for example Buckingham Palace, London, England) I'd like to display to users "This was the second event in history at Buckingham Palace, the 8th in London and 12th in England, or whatever they'd be.
So what I've done is get all the shows up to and including this one in date order:
//get the date of the selected event
$eventDate = $this->date->format('Y-m-d');
//get all events up to and including this date
$allEvents = Event::orderBy('date')->where('date', '<=', $eventDate)->get();
Then I'm querying that collection for the counts:
$countVenue = $allEvents->where('venue', $this->venue)->count();
$countCity = $allEvents->where('city', $this->city)->count();
$countCountry = $allEvents->where('country_code', $this->country_code)->count();
I'm still learning Laravel and Eloquent and I feel like my approach is probably not the best one. It is obviously loading X number of records up to that date in the initial query, so for events close to the present day that's going to be something like 2,000 events/model counts.
I've tried to think of a better option but coming up blank. Any thoughts? Thanks.