In this episode, we dive into how to keep track of your batch jobs by creating a dedicated table in the database. We start off by making a new model called ServerTask
and setting up a migration to support it. This table will hold all the tasks that get pushed to the batch, and it lets us keep tabs on their order, job class, and a bunch of other important details for each job.
You'll walk through scaffolding out the ServerTask
model, setting up relationships, and making sure it all links back to the right server. Along the way, we also make a trait to help collect and build all the job tasks so that they're ready to be stored in the database in one go—keeping everything fast and efficient.
Once the model and relationships are set up, we'll check everything's working by dumping out the tasks and then actually saving them to the database. At the end, you'll see how you now have easy access to all your batch tasks, ready for progress tracking or display in your app.
Next up, we'll enhance these tasks by adding a state field, so you can visually and programmatically track jobs as they move between pending, in-progress, and completed! Stay tuned for how to handle model states in Laravel in the following episode.