In this episode, we dive into batching jobs in Laravel and why you might want to use them. Instead of dispatching single jobs one after the other (where each job isn't really aware of the others), you'll learn how batching can group multiple jobs together. This makes it possible to run several steps in a specific order and manage them as a single unit—perfect for something like setting up a server where you want to create a server, install Nginx, install PHP, and then finalize it, all in sequence.
We start by creating individual job classes for each step, and then tweak them to be "batchable" by using Laravel's Batchable
trait. Then, you'll see how to form these jobs into a batch array and dispatch them all at once using the Laravel Bus facade. As the batch runs, you can monitor its status, see which jobs are running or have failed, and even cancel the whole batch if needed (super useful for things like a user clicking 'Cancel' while the process is running).
After setting up the batch, you'll see it in action, with jobs running one after another. We'll also check out how the status and progress of batches are stored in the database, making it easy to keep track of everything. By the end of the episode, you'll be ready to use job batching in your own Laravel apps, and we give a quick preview of how you'll make this even easier with model states in future sections.