Dynamic relationships in Laravel using subqueries Developers are typically pretty good at the first goal. We're aware of N+1 style problems, and use techniques like eager-loading to limit database queries. However, we're not always the best at the second goal—keeping memory usage down. In fact, we sometimes do more harm than good trying to reduce database queries at the expense of memory usage.
Let me explain how this happens, and what you can do to satisfy both goals in your app.