Laravel
/
Dary nazar
How to improve Laravel eager loading by selecting specific columns with the with() method
If you've been building Laravel apps for a while, you've probably run into the classic N+1 query problem. Lazy loading is convenient, but it kills performance when you loop through a collection and access relationships one by one.

If you've been building Laravel apps for a while, you've probably run into the classic N+1 query problem. Lazy loading is convenient, but it might kill the performance when you loop through a collection and access relationships one by one.
The fix? Eager loading with with(). But here's the thing most tutorials skip: by default, with('user') loads every single column from the users table. With APIs, mobile apps, and large datasets, this is a silent performance killer.
What is lazy loading? (Quick refresher + the N+1 problem)
When you access a relationship as a property for the first time, Laravel runs a lazy query:
This fires 1 query for posts + N queries for users → disaster at scale.
Eager loading fixes it, since it performs 2 queries total: posts + all users.
This is great…. but with('user') still pulls the entire users table row, and this can be done better.
Eager loading specific columns
Laravel lets you specify exactly which columns to load using the super-clean colon syntax:
That's it! Laravel will only select id, name, and email from the users table.
Why selective column eager loading works well
By telling Laravel exactly which columns to fetch from related models (e.g., with('user:id,name,email'), you unlock serious performance and efficiency gains.
Much smaller payload
Especially critical for APIs loading full user models often means sending dozens of unnecessary columns (remember_token, email_verified_at, timestamps, etc.). Smaller payloads = faster API responses, lower bandwidth costs, and happier mobile users who aren't waiting on bloated data.
Less memory pressure in PHP
When Laravel hydrates full Eloquent models for hundreds or thousands of records, PHP has to allocate memory for every column, even the ones you ignore. By loading only 3–5 fields, you dramatically reduce memory usage, helping you avoid those dreaded "Allowed memory size exhausted" errors on production servers during peak traffic.
Less data transferred over the wire
Every byte counts in 2026: slower networks, metered mobile data, CDNs charging by GB. Sending only what your frontend actually needs reduces latency, improves Time to First Byte (TTFB), and makes your app feel snappier worldwide.
Always include the foreign key / primary key
If you forget the id (or custom primary key), the relationship won't match up correctly, you'll get empty or null relations.
Correct example (belongsTo):
Wrong example:
Another example where it has been setup correctly:
Your JSON response is now tiny compared to dumping full user models.
An alternative: the closure way
Sometimes you don’t just want to select specific columns, you also need to add conditions, ordering, limits, or even apply model scopes to the related query. That’s where the closure syntax shines. It gives you the full power of the query builder while still letting you keep things lean.
Conclusion
Selective eager loading isn't just a "nice-to-have". In real apps with real users, it's often one of the quickest ways to 2–10× faster APIs and happier everything. Try measuring your own endpoints before/after, the numbers will convince you instantly.
Laravel


