Chunk a large export in a job

chunkById walks a table in a job. Do not chunk() an update, and do not do this work inside the HTTP request.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/laravel-chunk-export-vue.md
chunkById walks a table in a job. Do not chunk() an update, and do not do this work inside the HTTP request.
Do not chunk() an update, and do not do this work inside the HTTP request.
Do not Post::all() and then collect()->chunk() to feel safer.
Do not start this from a Vue setInterval.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel chunk export” call chunk() inside a controller and the request times out. A download the user is waiting on should stream, which is the CSV page’s job. A file you email later belongs in a queued job. The queue guide is how that job gets on the page.

TL;DR

  • chunkById walks a table in a job.
  • Do not chunk() an update, and do not do this work inside the HTTP request.
  • Do not Post::all() and then collect()->chunk() to feel safer.
  • Do not start this from a Vue setInterval.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
Post::query()->orderBy('id')->chunkById(200, function ($posts) {
    foreach ($posts as $post) {
        // append a row to the export, or dispatch a smaller job
    }
});

chunkById remembers the last id. chunk() uses offset. If you delete or update rows inside chunk(), pages get skipped. That is the bug.

What to avoid#

Gotcha

Do not Post::all() and then collect()->chunk() to feel safer. You already loaded the table. Do not start this from a Vue setInterval. The button dispatches one job.

FAQ#

How do I chunk a large export in a job?

chunkById walks a table in a job. Do not chunk() an update, and do not do this work inside the HTTP request.

What goes wrong with Laravel chunk export?

Many developers call chunk() inside a controller and the request times out. Do not Post::all() and then collect()->chunk() to feel safer.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.