In this episode, we're taking a closer look at how to enforce eager loading in our Laravel applications and why you might want to do that. We already know that eager loading related models makes our queries more efficient, but sometimes it's easy to forget to use it, which leads to the less efficient lazy loading.
We'll see a neat Laravel trick where you can tell the framework to completely prevent lazy loading. This means any time you forget to eager load a relationship, Laravel will throw a nice big error—making it much easier to spot and fix accidentally expensive queries during development.
The episode walks through exactly where and how to set this up (spoiler: it's in your AppServiceProvider), and you'll see how just removing one line of eager loading instantly triggers a 'lazy loading violation' exception. This is a great habit to get into for new projects to keep things fast from the beginning.
There's one catch, though: if you push this straight to production, those errors would crash your user's page. So, we'll make things smarter by only preventing lazy loading in development—not production. That way, you get all the dev-time safety, but your users won't see errors if something slips through. Now you can develop with confidence, knowing you're catching inefficient queries early while still keeping your app stable in production.