0
0
Laravelframework~5 mins

Job chaining and batching in Laravel

Choose your learning style9 modes available
Introduction

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.

You want to send emails one after another, not all at once.
You need to process images step-by-step, like resize then watermark.
You want to run many tasks and know when all finish.
You want to handle errors if one task in a chain fails.
You want to group jobs and track their progress as a batch.
Syntax
Laravel
<?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.

Examples
This runs SendWelcomeEmail first, then UpdateUserStats, then LogUserActivity.
Laravel
<?php

// Chain with three jobs
Bus::chain([
    new SendWelcomeEmail,
    new UpdateUserStats,
    new LogUserActivity,
])->dispatch();
This runs GenerateReport and SendReportEmail together and calls the then callback when all finish.
Laravel
<?php

// Batch with jobs
Bus::batch([
    new GenerateReport,
    new SendReportEmail,
])->then(function (Batch $batch) {
    // All jobs done
})->dispatch();
Even with one job, chaining works and runs that job.
Laravel
<?php

// Chain with one job
Bus::chain([
    new SingleJob,
])->dispatch();
Batch with no jobs runs immediately and finishes.
Laravel
<?php

// Batch with no jobs
Bus::batch([])->dispatch();
Sample Program

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).

Laravel
<?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
OutputSuccess
Important Notes

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.

Summary

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.