Laravel - Collections - Creating Lazy Collections
To create a lazy collection instance, you should pass a PHP generator function to the collection's make
method:
use Illuminate\Support\LazyCollection;
LazyCollection::make(function () {
$handle = fopen('log.txt', 'r');
while (($line = fgets($handle)) !== false) {
yield $line;
}
});