Video Version of the Lesson
[Only for premium members]
[Only for premium members]
[Only for premium members]
When building Laravel applications that serve as APIs, the amount of data transferred over the network is equally critical for performance. This lesson explores how to minimize API response size.
Even with server-side optimizations like query limits in place, APIs often return excessive data that clients don't actually need. This creates several issues:
Consider a simple API endpoint returning posts with their associated users:
// Unoptimized approachreturn Post::with('user') ->latest() ->take(100) ->get();
Testing this endpoint with an API client reveals:
The issue? We're returning every column from both tables, including lengthy post content and user descriptions that the client doesn't need.
We can limit post only to...