Use lazy() Instead of get() for Large Datasets
- Eloquent
- Laravel
When processing a lot of rows, lazy() can dramatically reduce memory usage:
1User::lazy()->each(function ($user) {2 // Do something with each user3});
It loads the records one by one using a cursor under the hood.