๐—ฃ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€๐—ถ๐—ป๐—ด ๐— ๐—ถ๐—น๐—น๐—ถ๐—ผ๐—ป๐˜€ ๐—ช๐—ถ๐˜๐—ต๐—ผ๐˜‚๐˜ ๐— ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜† ๐—–๐—ฟ๐—ฎ๐˜€๐—ต๐—ฒ๐˜€

You process large data sets in Laravel. You load 500,000 rows into memory. Your server crashes. You see a memory exhausted error.

Standard Eloquent collections load all records at once. This uses too much RAM. It stops your app.

Use Lazy Collections. They use PHP Generators. They load one row at a time. Memory stays flat.

Swap your query:

The data stays in the database. It enters RAM when you need it. It leaves RAM after you process it.

Your server handles more load. Your memory usage stays low. Your system scales.

Source: https://dev.to/iprajapatiparesh/processing-millions-without-memory-crashes-laravel-lazy-collections-34e9