January 22nd, 2024

Conditionally dispatch job chains and batches

Conditionally dispatch job chains and batches

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);
Statamic Ninja

Comments

Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.