In this episode, we're diving into eager loading and why it's important for our app's performance, especially on the homepage where we list a bunch of endpoints and related data. Up until now, we haven't been using eager loading, which means every time we loop over relationships, we're running lots of unnecessary queries—cue the infamous N+1 problem!
To help us spot where these queries are happening, we bring in Laravel Debugbar. It's a super handy tool that shows us how many database queries are being run on each page load. Right away, we can see the query count is way too high, and as we add more endpoints, it just keeps getting worse.
So, we start fixing this by eager loading the relationships our dashboard relies on. We use the load
method to fetch related site
, endpoints
, and the various checks
(plus the latest one) all in one go. Each time we add a relationship to eager load, we refresh the page and watch the query count drop in Debugbar. By the end, we've reduced it to a much more reasonable number, and we've completely solved the issue where more endpoints meant way more queries.
That's pretty much eager loading in a nutshell! We make our app more efficient and set ourselves up so adding more data won't slow things down.