In this episode, we dive into batching CSV imports using Laravel's job batching features. We start by creating a new job called ImportCSV
and making it batchable with the Batchable
trait. At this point, we're not stuffing the handle method with any real logic yet—just some basic logging so we know the batch is running.
From there, we talk about chunking the CSV records into batches (like groups of 10), dynamically building up an array of jobs depending on how many chunks we have. So, for a 100-row import, we'll get 10 jobs (with 10 records each), and if there's a remainder, the last job will just have those extra records.
We go through the process of wiring up the batch dispatch with the Bus facade, showing how each chunk becomes a new ImportCSV
job. There's some live coding as we upload a file, check the database for batch metadata, and look at the jobs queued up in Redis and displayed in Laravel Horizon.
Finally, we kick off the worker and watch the logs update in real time as the queued jobs get processed. The episode wraps up with the batch running as expected, ready for us to finish up the actual import logic inside the job itself next time.