Laravel version 10.41.0 introduced a feature that allows for the conditional dispatch of job chains and batches, similar to how it's done for individual jobs. This enhancement simplifies logic and enhances the clarity of the code. Importantly, these additions do not affect current implementations of job chains and are designed to be backward compatible.
Frankie's PR / PR introduces dispatchIf() and dispatchUnless() methods to the PendingChain and PendingBatch classes.
Dispatch a job chain conditionally:
1Bus::chain([ 2 new JobA(), 3 new JobB(), 4 new JobC(), 5])->dispatchIf(true); 6 7Bus::chain([ 8 new JobA(), 9 new JobB(),10 new JobC(),11])->dispatchUnless(false);
Dispatch a job batch conditionally:
1Bus::batch([ 2 new JobA(), 3 new JobB(), 4 new JobC(), 5])->dispatchIf(true); 6 7Bus::batch([ 8 new JobA(), 9 new JobB(),10 new JobC(),11])->dispatchUnless(false);
Written by
Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.
Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.