What is the difference between laravel cursor and laravel chunk method?

What is the difference between laravel cursor and laravel chunk method?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What Is The Difference Between Laravel Cursor And Chunk Methods?

When handling large datasets in a Laravel application, you quickly realize that fetching all results into memory is a recipe for disaster. Whether you are building an export feature or migrating data, you need to process records in batches. This is where chunk and cursor come into play. While both methods solve the memory problem of loading thousands of rows at once, their underlying mechanics and ideal use cases differ significantly.

The chunk() method executes a SQL query that retrieves all records matching your criteria, then processes them in batches within PHP. It returns an array of results for each chunk you define. Under the hood, Laravel uses a standard LIMIT and OFFSET strategy to paginate the results on the database side, but it still constructs a full result set from the perspective of the query builder before handing off control to your closure.

Flight::chunk(200, function ($flights) {
    foreach ($flights as $flight) {
        // Process flight data
    }
});

This approach is straightforward and works well for standard iteration. However, the trade-off is performance on very large tables. Because chunk relies on OFFSET, the database must scan and skip over previous rows to reach the start of each new batch. As the offset grows deeper into a massive table, this scanning becomes increasingly expensive, leading to slower query times as you progress through the dataset.

In contrast, cursor() takes a different architectural approach designed specifically for streaming large datasets efficiently. It does not load a full array into memory; instead, it creates a database-level cursor (a pointer) that keeps track of its position in the result set.

foreach (Flight::where('foo', 'bar')->cursor() as $flight) {
    // Process flight data immediately without loading bulk data
}

The primary advantage of cursor is its ability to maintain a constant memory footprint regardless of how many records exist in the database. It fetches one record, yields it to your closure, and then advances the pointer before fetching the next. This eliminates the OFFSET penalty entirely. The query logic shifts from "skip N rows, take M rows" to a continuous stream where the database engine manages the state of traversal.

When To Use Which?

Choosing between these two methods depends on your specific workflow requirements.

Use chunk() when you need random access to items within a batch or when performing operations that require multiple records per iteration. For example, if you are updating a table based on relationships where you need to join data from the current chunk with another related model, chunk is often more convenient because it gives you a collection object for the duration of that loop. It also simplifies code when you need to break early or return a value after processing a specific number of rows within a batch.

Use cursor() when dealing with extremely large tables where performance is critical, such as migrating millions of records or generating massive reports. Since cursor avoids the cumulative cost of increasing offsets, it maintains consistent query speed throughout the entire operation. It is also essential when you are strictly reading data to write it elsewhere (like an external API) without needing to hold multiple rows in PHP memory simultaneously.

A common pitfall with chunk is forgetting that the closure receives a collection, meaning you can call methods like count() or pluck() on $flights. With cursor, you are strictly iterating row-by-row, so attempting to access collection-level properties will fail because no collection is constructed. Understanding this distinction ensures you write code that scales predictably as your data grows.

Our network