Laravel Tutorials

Cleaner Queue Chains with Laravel's Enum Integration

Published
Cleaner Queue Chains with Laravel's Enum Integration image

Laravel now supports direct use of backed enums with the Bus facade's onQueue method, eliminating the need to manually access the enum's value property. This improvement creates cleaner, more expressive code when working with job chains and queues.

This enhancement is particularly useful when building complex job pipelines that need to be directed to different queues based on priority, resource requirements, or business logic.

use App\Enums\QueueType;

// Use enum directly without ->value
Bus::chain($jobs)
    ->onQueue(QueueType::Background)
    ->dispatch();

Here's a practical example of implementing a document processing system:

<?php

namespace App\Enums;

enum ProcessingQueue: string
{
    case Immediate = 'realtime';
    case Standard = 'default';
    case Batch = 'batch-process';
    case LowPriority = 'low-priority';
}

namespace App\Services;

use App\Enums\ProcessingQueue;
use App\Jobs\ProcessDocument;
use App\Jobs\GenerateThumbnail;
use App\Jobs\ExtractMetadata;
use App\Jobs\NotifyUser;
use App\Models\Document;
use Illuminate\Support\Facades\Bus;

class DocumentProcessor
{
    public function process(Document $document, bool $isPriority = false)
    {
        $queue = $isPriority 
            ? ProcessingQueue::Immediate 
            : ProcessingQueue::Standard;
            
        Bus::chain([
            new ProcessDocument($document),
            new ExtractMetadata($document),
            new GenerateThumbnail($document),
            new NotifyUser($document->user, 'Document processing complete')
        ])
        ->onQueue($queue)
        ->dispatch();
        
        return $document;
    }
    
    public function batchProcess(array $documentIds)
    {
        foreach ($documentIds as $id) {
            $document = Document::find($id);
            
            Bus::chain([
                new ProcessDocument($document),
                new GenerateThumbnail($document)
            ])
            ->onQueue(ProcessingQueue::Batch)
            ->dispatch();
        }
    }
}

This enhancement simplifies your queue implementation while maintaining type safety and improving code readability.

Harris Raftopoulos photo

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

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Session Replay for Laravel: Self-Hosted Browser Recordings image

Session Replay for Laravel: Self-Hosted Browser Recordings

Read article
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