Shamim Shams
Shamim Shams
Published on

SELECT ONLY FIELDS YOU NEED IN ELOQUENT EAGER LOADED TABLE

Authors

Most of the time we select all fields in eager-loaded fields from eloquent eager-loaded fields which causes unnecessary memory consumption. Using a simple trick we can select only the necessary field from eager loaded table and reduce memory consumption.

User::select('id', 'name', 'email' )->with('post:id,title, description')->get();

In the above code, we are only selecting id, title, and description from the posts table.