Laravel's --batched Flag: Instant Job Batching Setup

Laravel's make:job
command now includes a --batched
option that automatically scaffolds everything you need for job batching. Gone are the days of remembering which traits to import and which methods to implement.
The Old Manual Process vs. The New Streamlined Approach
Previously, setting up a batchable job required multiple manual steps. You'd create a regular job and then manually add the necessary traits and boilerplate:
// Step 1: Create the job
php artisan make:job ProcessPodcast
// Step 2: Manually add traits and cancellation logic
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class ProcessPodcast implements ShouldQueue
{
use Batchable, Queueable; // Remember to add these
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}
// Your logic here
}
}
Now, a single command with the --batched
flag generates everything you need:
php artisan make:job ProcessPodcast --batched
This automatically creates a fully-configured batchable job with all the necessary traits and cancellation handling built-in.
Real-World Example
Imagine you're building a podcast platform that needs to process multiple audio files when a new season is uploaded. Each episode requires transcription, thumbnail generation, and metadata extraction:
<?php
namespace App\Jobs;
use App\Models\Episode;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Bus;
class ProcessEpisode implements ShouldQueue
{
use Batchable, Queueable;
public function __construct(
public Episode $episode
) {}
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}
// Process the episode
$this->generateTranscript();
$this->createThumbnail();
$this->extractMetadata();
$this->episode->update(['status' => 'processed']);
}
private function generateTranscript(): void
{
// AI transcription logic
}
private function createThumbnail(): void
{
// Image processing logic
}
private function extractMetadata(): void
{
// Metadata extraction logic
}
}
class SeasonController
{
public function processSeason(Season $season)
{
$jobs = $season->episodes->map(
fn($episode) => new ProcessEpisode($episode)
);
$batch = Bus::batch($jobs)
->name("Process Season: {$season->title}")
->onQueue('media-processing')
->dispatch();
return response()->json([
'batch_id' => $batch->id,
'total_jobs' => $batch->totalJobs
]);
}
}
The --batched
flag ensures your job class includes the critical cancellation check from the start. This prevents partially processed episodes when a batch is cancelled, maintaining data consistency across your application.
The generated boilerplate also makes it immediately clear that this job is designed for batch processing, improving code readability and team understanding. New developers can instantly recognize the job's batching capabilities without diving into the implementation details.
Stay Updated with More Laravel Tips
Enjoyed this article? There's plenty more where that came from! Subscribe to our channels to stay updated with the latest Laravel tips, tricks, and best practices: