In this episode, we dive into one of Laravel's cool new features: skip middleware for queued jobs. If you've ever found yourself cluttering up your handle
method with early returns—"if the job shouldn't run, just bail early!"—you'll appreciate this cleaner approach.
We kick things off by building a super simple example around a Podcast
model and a job to process podcasts. The model just has a processed timestamp—if it's filled in, the podcast has already been processed. We wire up the job to update this timestamp when it's done and check in the database to make sure our logic is working.
Before skip middleware, you'd add an if
statement near the top of your job's handle
method to check if the job should run, which gets messy if you have multiple jobs or conditions. However, now we can move that logic over to a dedicated middleware
method using Illuminate\Queue\Middleware\SkipWhen
, making things a lot neater. You get to define your condition there, or even use a closure for more complex scenarios.
By the end of the episode, you'll see how this approach not only keeps your job's code nice and tidy, but also centralizes your "should this job run?" logic—right where it belongs. So, if you're looking for a better way to manage when jobs should be skipped in the queue, this episode is for you!