Job chaining and batching help you run tasks in order or as a group. This makes sure tasks happen one after another or all together.
Job chaining and batching in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
<?php
use Illuminate\Bus\Batch;
use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
// Job chaining example
Bus::chain([
new FirstJob,
new SecondJob,
new ThirdJob,
])->dispatch();
// Job batching example
Bus::batch([
new JobOne,
new JobTwo,
new JobThree,
])->then(function (Batch $batch) {
// All jobs completed successfully
})->catch(function (Batch $batch, Throwable $e) {
// Handle failure
})->dispatch();Use Bus::chain() to run jobs one after another.
Use Bus::batch() to run jobs together and track them.
SendWelcomeEmail first, then UpdateUserStats, then LogUserActivity.<?php
// Chain with three jobs
Bus::chain([
new SendWelcomeEmail,
new UpdateUserStats,
new LogUserActivity,
])->dispatch();GenerateReport and SendReportEmail together and calls the then callback when all finish.<?php
// Batch with jobs
Bus::batch([
new GenerateReport,
new SendReportEmail,
])->then(function (Batch $batch) {
// All jobs done
})->dispatch();<?php
// Chain with one job
Bus::chain([
new SingleJob,
])->dispatch();<?php
// Batch with no jobs
Bus::batch([])->dispatch();This example shows three jobs defined with simple handle methods that print messages. It dispatches them as a chain, so they run one after another. Then it dispatches the same jobs as a batch, which runs them together and prints a message when all finish.
Note: To see the output, you must run Laravel's queue worker (e.g., php artisan queue:work).
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class FirstJob implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; public function handle() { echo "First job done\n"; } } class SecondJob implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; public function handle() { echo "Second job done\n"; } } class ThirdJob implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; public function handle() { echo "Third job done\n"; } } use Illuminate\Support\Facades\Bus; use Illuminate\Bus\Batch; use Throwable; use App\Jobs\FirstJob; use App\Jobs\SecondJob; use App\Jobs\ThirdJob; // Dispatch chained jobs echo "Dispatching chained jobs...\n"; Bus::chain([ new FirstJob, new SecondJob, new ThirdJob, ])->dispatch(); // Dispatch batch jobs $batch = Bus::batch([ new FirstJob, new SecondJob, new ThirdJob, ])->then(function (Batch $batch) { echo "Batch completed successfully\n"; })->catch(function (Batch $batch, Throwable $e) { echo "Batch failed: " . $e->getMessage() . "\n"; })->dispatch(); // Output will appear as jobs run in queue worker
Job chaining runs jobs one by one, waiting for each to finish before starting the next.
Job batching runs jobs in parallel and lets you track when all finish or if any fail.
Common mistake: forgetting to run the queue worker, so jobs never execute.
Use chaining when order matters. Use batching when you want to run many jobs and track them.
Job chaining runs tasks in a set order, one after another.
Job batching runs many tasks together and tracks their progress.
Both help manage background work easily and reliably in Laravel.
Practice
job chaining and job batching in Laravel?Solution
Step 1: Understand job chaining
Job chaining runs jobs one after another in a specific order.Step 2: Understand job batching
Job batching runs many jobs together and tracks their progress as a group.Final Answer:
Job chaining runs jobs one after another; job batching runs jobs together and tracks progress. -> Option CQuick Check:
Job chaining = sequential, Job batching = grouped with progress [OK]
- Confusing parallel and sequential execution
- Thinking batching runs jobs one by one
- Assuming chaining tracks progress automatically
JobA and JobB in Laravel?Solution
Step 1: Recall chaining syntax
In Laravel, chaining jobs usesdispatch(new JobA)->chain([new JobB]).Step 2: Check options for correct method calls
dispatch(new JobA)->chain([new JobB]);matches the correct syntax withdispatch(new JobA)->chain([new JobB]);.Final Answer:
dispatch(new JobA)->chain([new JobB]); -> Option DQuick Check:
dispatch + chain array = correct chaining syntax [OK]
- Calling chain() on the job class instead of dispatch
- Passing a single job without array to chain()
- Using incorrect method order
Bus::batch([
new Job1(),
new Job2(),
new Job3()
])->then(function () {
Log::info('Batch completed successfully');
})->dispatch();Solution
Step 1: Understand batch then() callback
Thethen()callback runs after all jobs in the batch succeed.Step 2: Analyze the code behavior
Since all jobs succeed, the callback logs 'Batch completed successfully' after finishing.Final Answer:
Logs 'Batch completed successfully' after all jobs finish. -> Option BQuick Check:
Batch then() runs after success [OK]
- Thinking then() runs immediately after dispatch
- Assuming then() runs on failure
- Believing no logs are created
dispatch(new JobA)->chain(new JobB);
Solution
Step 1: Check chain() parameter requirements
Thechain()method requires an array of job instances.Step 2: Analyze the code
The code passes a single job instancenew JobBwithout wrapping it in an array.Final Answer:
chain() expects an array of jobs, not a single job instance. -> Option AQuick Check:
chain() needs array of jobs [OK]
- Passing a single job without array to chain()
- Trying to dispatch chained jobs separately
- Confusing dispatch and chain usage
JobX, JobY, and JobZ where JobY should only run after JobX completes, but JobZ can run anytime in parallel. Which Laravel approach correctly handles this?Solution
Step 1: Understand job dependencies
JobY depends on JobX finishing first, so they must run sequentially.Step 2: Consider JobZ's independence
JobZ can run anytime, so it should run separately in parallel.Step 3: Choose correct Laravel approach
Use chaining for JobX and JobY, and dispatch JobZ separately to run in parallel.Final Answer:
Use job chaining for JobX and JobY, and dispatch JobZ separately. -> Option AQuick Check:
Chain dependent jobs, dispatch independent jobs separately [OK]
- Batching all jobs when order matters only for some
- Chaining all jobs forcing sequential run
- Ignoring job dependencies
