In this episode, we dive into the basics of using queues in Laravel, which is key for handling long-running tasks in the background without slowing down your application or your user's experience. We kick things off by looking at how Laravel comes with built-in support for database queues, and check out the jobs
table where all the queued jobs are stored.
You'll see how to create a simple job class using php artisan make:job
, get familiar with what happens in the constructor and the all-important handle()
method, and understand how queued jobs can make your app much more responsive. For the example, we literally just make the job “sleep” for five seconds, just to simulate a long task.
We then wire up a quick route to dispatch our job to the queue, and explore a couple of easy ways to do this—either using the dispatch()
helper or the static dispatch()
method on the job class itself. Once dispatched, we jump into the database to see how the job is serialized and stored, peek into its payload, and talk through what all that info’s for.
You'll also see how to kick off your queue worker locally using php artisan queue:work
, and learn about some gotchas, like needing to restart your worker if you change your job's code. We finish up by watching our queued job get processed, with the five-second delay and all, before it disappears from the queue.
By the end, you'll have a solid handle on how database queues work in Laravel and be set up for the next episode, where we'll start batching jobs together as a unit. Stay tuned!