Eloquent Cursor Pagination &amp; Lazy Collections at Scale | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Cursor Pagination, Chunked Iteration, and Lazy Collections at Scale in Laravel        On this page       1. [  The Problem With Offset at Scale ](#the-problem-with-offset-at-scale)
2. [  Cursor Pagination ](#cursor-pagination)
3. [  Gotchas ](#gotchas)
4. [  Chunked Iteration ](#chunked-iteration)
5. [  Lazy Collections ](#lazy-collections)
6. [  Combining Lazy Collections With Chunking ](#combining-lazy-collections-with-chunking)
7. [  Choosing the Right Tool ](#choosing-the-right-tool)
8. [  Key Takeaways ](#key-takeaways)

  ![Cursor Pagination, Chunked Iteration, and Lazy Collections at Scale in Laravel](https://cdn.msaied.com/355/3a6df23a2c16b740843260134fad7c63.png)

  #laravel   #eloquent   #performance   #database   #pagination  

 Cursor Pagination, Chunked Iteration, and Lazy Collections at Scale in Laravel 
================================================================================

     3 Jul 2026      4 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   The Problem With Offset at Scale  ](#the-problem-with-offset-at-scale)
2. [  02   Cursor Pagination  ](#cursor-pagination)
3. [  03   Gotchas  ](#gotchas)
4. [  04   Chunked Iteration  ](#chunked-iteration)
5. [  05   Lazy Collections  ](#lazy-collections)
6. [  06   Combining Lazy Collections With Chunking  ](#combining-lazy-collections-with-chunking)
7. [  07   Choosing the Right Tool  ](#choosing-the-right-tool)
8. [  08   Key Takeaways  ](#key-takeaways)

 The Problem With Offset at Scale
--------------------------------

Offset-based pagination (`LIMIT 100 OFFSET 50000`) forces the database to scan and discard 50,000 rows before returning your page. On a table with millions of rows and a busy write workload, that scan gets expensive fast — and the query plan degrades non-linearly.

Laravel gives you three tools to escape this trap: **cursor pagination**, **chunked iteration**, and **lazy collections**. Each solves a different problem. Reaching for the wrong one wastes either memory or database round-trips.

---

Cursor Pagination
-----------------

Cursor pagination encodes the last-seen row's ordered column value into an opaque token. The next page becomes a `WHERE` clause rather than an `OFFSET`.

```php
// Route handler
$users = User::orderBy('id')
    ->cursorPaginate(50);

return UserResource::collection($users);

```

The generated SQL looks like:

```sql
SELECT * FROM users
WHERE id > 3847
ORDER BY id ASC
LIMIT 51; -- one extra to detect next page

```

The cursor itself is a base64-encoded JSON payload containing the column values at the boundary. Laravel's `CursorPaginator` handles encoding and decoding transparently.

### Gotchas

- **You must order by a unique, indexed column** (or a combination that is effectively unique). Non-unique cursors produce inconsistent pages.
- Cursor pagination does not support jumping to arbitrary pages — it is forward/backward only. If your UI needs page numbers, stick with `paginate()`.
- When ordering by a non-unique column (e.g., `created_at`), add `id` as a tiebreaker: `->orderBy('created_at')->orderBy('id')`.

```php
$orders = Order::orderBy('created_at')->orderBy('id')
    ->cursorPaginate(25);

```

Laravel will encode both columns into the cursor token automatically.

---

Chunked Iteration
-----------------

When you need to process every row in a table — exports, migrations, backfills — loading everything into memory at once is fatal. `chunk()` issues repeated queries, each fetching a fixed batch.

```php
User::orderBy('id')->chunk(1000, function (Collection $users) {
    foreach ($users as $user) {
        ProcessUser::dispatch($user);
    }
});

```

Under the hood this is still offset-based, so on very large tables the later chunks slow down. Use `chunkById()` instead — it rewrites each batch as a keyed `WHERE id > ?` query:

```php
User::chunkById(1000, function (Collection $users) {
    $users->each(fn ($u) => ProcessUser::dispatch($u));
});

```

`chunkById()` requires an ordered, unique column (defaults to the model's primary key). It is safe to modify rows inside the callback because the cursor advances by ID, not by offset.

---

Lazy Collections
----------------

`LazyCollection` wraps a PHP generator, pulling one row at a time from the database cursor. Memory usage stays flat regardless of result set size.

```php
User::cursor()->each(function (User $user) {
    // Only one User model in memory at a time
    $user->recalculateScore();
    $user->saveQuietly();
});

```

The underlying PDO fetch mode is set to `PDO::FETCH_LAZY`, and Eloquent hydrates one model per iteration. This is ideal for read-heavy pipelines where you do not need to batch database writes.

### Combining Lazy Collections With Chunking

For write-heavy pipelines, combine `cursor()` with `chunk()` on the `LazyCollection` to batch inserts while keeping memory low:

```php
User::cursor()
    ->chunk(500)
    ->each(function (LazyCollection $batch) {
        $records = $batch->map(fn ($u) => [
            'user_id' => $u->id,
            'score'   => $u->computeScore(),
        ])->all();

        Score::upsert($records, ['user_id'], ['score']);
    });

```

This pattern keeps one chunk (500 models) in memory at a time while issuing a single `UPSERT` per batch.

---

Choosing the Right Tool
-----------------------

| Scenario | Tool | |---|---| | API pagination with forward/back navigation | `cursorPaginate()` | | Full-table processing, safe to modify rows | `chunkById()` | | Read-only streaming pipeline, minimal memory | `cursor()` / `LazyCollection` | | Batched writes over a large result set | `cursor()->chunk()` |

---

Key Takeaways
-------------

- `OFFSET` pagination degrades at scale; cursor pagination replaces it with an indexed `WHERE` clause.
- Always pair cursor pagination with a unique ordered column or a composite tiebreaker.
- `chunkById()` is safer than `chunk()` when modifying rows inside the callback.
- `LazyCollection::cursor()` holds one model in memory at a time — ideal for streaming reads.
- Combine `cursor()->chunk()` for memory-efficient batched writes over millions of rows.

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fcursor-pagination-chunked-iteration-and-lazy-collections-at-scale-in-laravel-1&text=Cursor+Pagination%2C+Chunked+Iteration%2C+and+Lazy+Collections+at+Scale+in+Laravel) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fcursor-pagination-chunked-iteration-and-lazy-collections-at-scale-in-laravel-1) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Can I use cursor pagination with a non-unique sort column like `created\_at`?        Yes, but you must add a unique tiebreaker such as `id` to the `orderBy` chain. Laravel will encode both columns into the cursor token, ensuring stable page boundaries even when multiple rows share the same `created_at` value. 

      Q02  What is the difference between `chunk()` and `chunkById()` in Eloquent?        `chunk()` uses LIMIT/OFFSET internally and can return duplicate or skipped rows if the table is modified during iteration. `chunkById()` advances via a `WHERE id &gt; ?` clause, making it safe to insert, update, or delete rows inside the callback. 

      Q03  When does `LazyCollection::cursor()` outperform `chunkById()`?        `cursor()` is best for read-only streaming pipelines where you process one row at a time and do not need to batch writes. For write-heavy workloads, `chunkById()` or `cursor()-&gt;chunk()` reduces the number of individual database round-trips. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Job Batching, Chaining, and Rate-Limited Middleware in Laravel Queues](https://cdn.msaied.com/353/89d47dc6b618d5435f9d7f333b75e922.png) laravel queues jobs 

### Job Batching, Chaining, and Rate-Limited Middleware in Laravel Queues

Go beyond basic dispatch: learn how to compose Laravel job batches with callbacks, chain dependent jobs safely...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 3 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/job-batching-chaining-and-rate-limited-middleware-in-laravel-queues-2) [ ![Laravel Reverb: Building Presence Channels with Per-User State and Typed Events](https://cdn.msaied.com/352/9b3c490b8303fdc84442671965a3ee8a.png) laravel reverb websockets 

### Laravel Reverb: Building Presence Channels with Per-User State and Typed Events

Presence channels in Laravel Reverb go far beyond simple pub/sub. Learn how to track per-user state, broadcast...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 3 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/laravel-reverb-building-presence-channels-with-per-user-state-and-typed-events) [ ![Laravel Caching Strategies: Tags, Stampede Prevention, and Cache-Aside at Scale](https://cdn.msaied.com/351/ac186f395c8b0f7ae2e8ff230c61e323.png) laravel caching performance 

### Laravel Caching Strategies: Tags, Stampede Prevention, and Cache-Aside at Scale

Beyond simple remember() calls: how to use cache tags for granular invalidation, prevent cache stampedes with...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 3 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/laravel-caching-strategies-tags-stampede-prevention-and-cache-aside-at-scale-1) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
