Calling get() loads all matching rows into memory before filtering. Use lazy() to process database records lazily.
Chaining collection methods like filter() or map() after get() loads the entire dataset into RAM first. Using lazy() streams database records through collection operations on demand.
use App\Models\Order;
// BAD: Loads 500,000 orders into RAM before filtering
$expensive = Order::get()->filter(fn ($o) => $o->calculateTotal() > 1000);
// GOOD: Streams records through filter lazily without RAM exhaustion
$expensive = Order::lazy()->filter(fn ($o) => $o->calculateTotal() > 1000);
- get() executes SELECT * and instantiates all models immediately
- lazy() fetches records in chunks as collection pipeline elements demand
- Slashes RAM usage when applying complex collection filters to large datasets
Related Tips
View all tips →Stream Large Datasets: cursor() vs lazy() in Eloquent
cursor() hydrates single model instances sequentially using database cursors; lazy() streams records in chunks backed by LazyCollection.
Choose the Right Processing Method: chunk(), lazy(), chunkById(), lazyById()
Understand offset vs keyset pagination when processing large datasets to avoid missing records during updates.