Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

weeniebeenie's avatar

Is there a better way to determine which instance in time an event is from table (e.g 4th event in Chicago)

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.

1 like
5 replies
vincent15000's avatar
Level 63

You can effectively do what you show here, but the problem is that you are retrieving all events from the database.

Why no combine your different queries ?

For example for $countVenue.

$countVenue = Event::orderBy('date')->whereDate('date', '<=', $event->date)->where('venue', $event->venue)->count();

But you should perhaps have a venues table and use the primary key instead of storing the venue as a string ?

1 like
weeniebeenie's avatar

@vincent15000 Yeah that is my concern, for example if I'm calculating the latest event that happened today then it's going to be returning about 2000 records before I do the counting, which is surely not ideal.

The only other thing I thought of is perhaps I can periodically generate the counts somehow and store them directly in the main db table? New events aren't added that often, so it could be a job that ran every few days?

1 like
vincent15000's avatar

@weeniebeenie Each time a new event is created, you can effectively increment the counter and store the result into the database for this event, for example as additional fields in the events table. But what if 2 events are created (say event A and then event B in the same country, city, ...) and then event A is deleted. In this case the event B will have false counters.

According to me, the best solution seems to be the query I suggested you. Does it return 2000 results ? I don't think so, it just returns the count.

1 like
weeniebeenie's avatar

@vincent15000 Sorry yes you're right, I misread the rewritten query you posted initially. I notice now that there is no get() like my original attempt meaning the X thousand models are not returned at once like I was doing.

Thanks!

1 like

Please or to participate in this conversation.