Laravel - Getting Started - Streaming Results Lazily

The lazy method works similarly to the chunk method in the sense that, behind the scenes, it executes the query in chunks. However, instead of passing each chunk directly into a callback as is, the lazy method returns a flattened LazyCollection of Eloquent models, which lets you interact with the results as a single stream:

    
    use App\Models\Flight;
    
    foreach (Flight::lazy() as $flight) {
        //
    }
	

If you are filtering the results of the lazy method based on a column that you will also be updating while iterating over the results, you should use the lazyById method. Internally, the lazyById method will always retrieve models with an id column greater than the last model in the previous chunk:

    
    Flight::where('departed', true)
        ->lazyById(200, $column = 'id')
        ->each->update(['departed' => false]);