Laravel Tutorials

Simplified Batch Job Creation with Laravel's Enhanced Artisan Command

Published
Simplified Batch Job Creation with Laravel's Enhanced Artisan Command image

Laravel's job generation command now streamlines batch processing setup through an integrated flag option. This enhancement eliminates manual configuration steps while ensuring developers include essential batch handling patterns from the start.

Traditional batch job setup required multiple manual steps after initial job creation:

php artisan make:job ProcessDocument

use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class ProcessDocument implements ShouldQueue
{
    use Batchable, Queueable;
    
    public function handle(): void
    {
        if ($this->batch()->cancelled()) {
            return;
        }
        
        // Processing logic here
    }
}

The enhanced command generates complete batch-ready jobs automatically:

php artisan make:job ProcessDocument --batched

This single command creates fully configured classes with necessary traits and cancellation handling integrated throughout the implementation.

Building a comprehensive document processing system demonstrates the practical advantages of automated batch job scaffolding:

namespace App\Jobs;

use App\Models\Document;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class ProcessDocument implements ShouldQueue
{
    use Batchable, Queueable;

    public function __construct(
        public Document $document
    ) {}

    public function handle(): void
    {
        if ($this->batch()->cancelled()) {
            return;
        }

        $this->extractText();
        $this->generateThumbnail();
        $this->performOcr();
        
        $this->document->markAsProcessed();
    }

    private function extractText(): void
    {
        // 
    }

    private function generateThumbnail(): void
    {
        // 
    }

    private function performOcr(): void
    {
        // 
    }
}

class DocumentController
{
    public function processBatch(Collection $documents)
    {
        $jobs = $documents->map(
            fn($document) => new ProcessDocument($document)
        );

        $batch = Bus::batch($jobs)
            ->name("Document Processing Batch")
            ->onQueue('document-processing')
            ->dispatch();

        return response()->json([
            'batch_id' => $batch->id,
            'job_count' => $batch->totalJobs
        ]);
    }
}

The automated scaffolding ensures critical batch cancellation checks are present from project initialization, preventing inconsistent data states and incomplete processing scenarios that could arise from manual implementation oversights.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article