Exploring Laravel 10.33: New Number Helpers, Hex Validation, and Batch Testing

Laravel////2 min read

The Versatile Number Helper

Laravel version 10.33 introduced a robust Number helper that simplifies complex formatting into readable strings. This utility replaces manual calculations and locale-specific logic with a clean, fluent API. You can easily convert large integers into shorter, human-readable versions like "12.8 million" using the forHumans method.

use Illuminate\Support\Number;

// Basic formatting with locale support
$formatted = Number::format(12756800, locale: 'de');

// Human-readable abbreviations
$short = Number::forHumans(12756800, precision: 2); // "12.76 million"

The helper also includes built-in support for currencies, percentages, and file sizes. For instance, Number::fileSize(1024 * 1024) automatically returns "1 MB," handling the heavy lifting of byte conversion while allowing for custom precision.

Hex Color Validation Made Simple

Handling design-related inputs just became significantly safer. Nico contributed a new hex_color validation rule that ensures a given string is a valid hexadecimal color code. This is a massive improvement over basic string checks, as it verifies the structure and character range (0-9, A-F).

$validator = Validator::make($request->all(), [
    'color' => 'required|hex_color',
]);

The rule elegantly handles standard hex codes and those including an alpha channel (RGBA), rejecting invalid characters like "G" or incorrect lengths automatically.

Testing Job Batches in Chains

Complex asynchronous workflows often involve job batches nested inside larger job chains. Testing these used to be difficult, but Taylor Otwell added a specialized assertion to the Bus fake. The busChainBatch method allows you to inspect the contents of a batch within a chain using a closure.

Bus::fake();

// ... dispatch your chain ...

Bus::assertChain([
    FlushCache::class,
    Bus::chainBatch(function ($batch) {
        return count($batch->jobs) === 2 && 
               $batch->jobs[0]->podcastId === 1;
    }),
]);

Syntax Notes and Best Practices

When using the Number helper, always specify a locale if your application serves international users, as number separators vary drastically between regions like the US and Germany. For testing batches, remember that busChainBatch requires a boolean return; if your closure returns false, the entire test fails. This methodical approach ensures your background processes are not just running, but running with the correct payload.

Topic DensityMention share of the most discussed topics · 5 mentions across 5 distinct topics
Ken
20%· people
Laravel
20%· software
Nico
20%· people
Taylor%20Otwell
20%· people
Tinkerwell
20%· software
End of Article
Source video
Exploring Laravel 10.33: New Number Helpers, Hex Validation, and Batch Testing

The New Number Helper, A Hex Color Rule & Test Job Batches Inside Job Chains

Watch

Laravel // 8:43

The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.

2 min read0%
2 min read