In Laravel you can run multiple batches one after another including other jobs in a chain. For example think that the first batch is required to complete specific set of jobs to start second job. In such cases you can use job chains that include batches.
Syntax:
Bus::chain([
new jobClass, //normal job class
Bus::batch($jobs_array_1), //batchable jobs in a array 1 that run in parallel
Bus::batch($jobs_array_2) //batchable jobs in a array 2 that run in parallel
])->dispatch();
Example
Below shows a full example of using Batches in a job chain and its result.
Task: Imagine you have to generate report for 5 outlets. After creating reports for all the outlets you have to send notification for all those outlets. You can run report generation in first batch and then run all sending notification jobs in next batch.
First create a fresh Laravel application and two Laravel Jobs to create two batches. Use below artisan command to create two job classes.
php artisan make:job GenerateReport
php artisan make:job SendNotification
Open GenerateReport job class and add below code.
- Add Batachable trait.
- Accept outlet name using constructor.
- Add log information.
<?php
namespace App\Jobs;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
class GenerateReport implements ShouldQueue
{
use Queueable, Batchable;
/**
* Create a new job instance.
*/
public function __construct(public string $outletName) {}
/**
* Execute the job.
*/
public function handle(): void
{
sleep(1);
Log::info('Completed Report for '. $this->outletName);
}
}
Open SendNotification job class and add below code.
- Add Batchable trait.
- Accept outlet name on constructor.
- Log information.
<?php
namespace App\Jobs;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
class SendNotification implements ShouldQueue
{
use Queueable, Batchable;
/**
* Create a new job instance.
*/
public function __construct(public string $outletName) {}
/**
* Execute the job.
*/
public function handle(): void
{
sleep(1);
Log::info('Completed notification for '. $this->outletName);
}
}
Open web.php file and add below code.
- Create a array of outlets.
- Create two empty arrays to hold instances of generate report and notification sending jobs.
- Iterate outlets array and create generate report and notification sending array for batch processing.
use App\Jobs\GenerateReport;
use App\Jobs\SendNotification;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
//Log request
Log::info("Received a get request on root route");
//List of outlets
$outlets = ['outlet_1','outlet_2', 'outlet_3','outlet_4','outlet_5'];
//Batchable jobs array
$reportBatch = [];
$notificationBatch = [];
//Create instances of GenerateReport and SendNotification jobs
foreach($outlets as $outlet)
{
array_push($reportBatch, new GenerateReport($outlet));
array_push($notificationBatch, new SendNotification($outlet));
}
//Batch processing
Bus::chain([
Bus::batch($reportBatch),
Bus::batch($notificationBatch)
])->dispatch();
});
Run your Laravel application with two queue workers. Run below artisan command in two terminals to create two queue workers.
php artisan queue:work
Now you can check logs file.
- It will complete one batch after another.
- Jobs within a batch run in parallel for faster processing. As you can see some jobs run in same time.
[2025-01-03 12:30:20] local.INFO: Received a get request on root route
//Running first batch
[2025-01-03 12:30:22] local.INFO: Completed Report for outlet_1
[2025-01-03 12:30:23] local.INFO: Completed Report for outlet_2
[2025-01-03 12:30:24] local.INFO: Completed Report for outlet_3
[2025-01-03 12:30:24] local.INFO: Completed Report for outlet_4
[2025-01-03 12:30:25] local.INFO: Completed Report for outlet_5
//Running 2nd batch after completing the 1st batch
[2025-01-03 12:30:26] local.INFO: Completed notification for outlet_1
[2025-01-03 12:30:27] local.INFO: Completed notification for outlet_2
[2025-01-03 12:30:28] local.INFO: Completed notification for outlet_3
[2025-01-03 12:30:29] local.INFO: Completed notification for outlet_4
[2025-01-03 12:30:29] local.INFO: Completed notification for outlet_5