This week, the Laravel team released v10.33, with new number helper methods, test assertions about batches that exist in a job chain, a hex color validation rule, and more. Here is a bit more info about the new features introduced this week:
Number utility class
Caen De Silva contributed the Laravel Number Utility Class, which simplifies most number formatting needs. The number helper lives in the Illuminate\Support
package, giving you several new helpers. Here are a few examples:
Number::format(25) // 25Number::format(100000) // 100,000Number::format(123456789) // 123,456,789 Number::format(123456789, locale: 'en') // 123,456,789Number::format(123456789, locale: 'de') // 123.456.789Number::format(123456789, locale: 'sv') // 123 456 789 Number::percentage(25) // 25%Number::percentage((1/3) * 100, precision: 2) // 33.33%
See our post on the Laravel Number Utility Class to learn about the newest Laravel helper.
Make assertions about batches that exist in job chains
Taylor Otwell contributed improvements to the testability of batches that exist in a chain via a new Bus::chainedBatch
testing helper. Here's an example of how this works taken from Pull Request #49003:
// RouteRoute::get('/batch', function () { Bus::chain([ new TestJob, Bus::batch([ new TestJob, ]), new TestJob, ])->dispatch();}); // TestBus::fake(); $response = $this->get('/batch');$response->assertSuccessful(); Bus::assertChained([ new TestJob, Bus::chainedBatch(function ($batch) { return $batch->jobs->count() === 1; }), new TestJob,]);
See the new Testing Job Chains documentation for more details.
Pass the $validator as a parameter to closure-based rules
@shinsente contributed by adding the validator instance to as an argument in closure-based validation rules:
use Closure;use Illuminate\Support\Facades\Validator;use Illuminate\Support\LazyCollection;use Illuminate\Validation\Validator as ValidationValidator; $rules = [ 'price' => [ function (string $attribute, mixed $value, Closure $fail, ValidationValidator $validator) { if (!price_logic($value, $validator->attributes()['payment_country'] ?? null)) { $fail("The {$attribute} is invalid."); } }, ],];
color_hex
validation rule
Add Niko Peikrishvili contributed a hex_color
validation rule in which the field under validation must contain a valid color value in hexadecimal:
$v = Validator::make(['color' => '#fff'], ['color' => 'hex_color']);$v->passes(); // true $v = Validator::make(['color' => '#ggg'], ['color' => 'hex_color']);$v->passes(); // false
Andy Hinkle also contributed alpha channel support to the hex validation rule:
$v = Validator::make(['color' => '#FF000080'], ['color' => 'hex_color']);$v->passes(); // true
Release notes
You can see the complete list of new features and updates below and the diff between 10.32.0 and 10.33.0 on GitHub. The following release notes are directly from the changelog:
v10.33.0
- [10.x] Fix wrong parameter passing and add these rules to dependent rules by @kayw-geek in https://github.com/laravel/framework/pull/49008
- [10.x] Make Validator::getValue() public by @shinsenter in https://github.com/laravel/framework/pull/49007
- [10.x] Custom messages for
Password
validation rule by @rcknr in https://github.com/laravel/framework/pull/48928 - [10.x] Round milliseconds in database seeder console output runtime by @SjorsO in https://github.com/laravel/framework/pull/49014
- [10.x] Add a
Number
utility class by @caendesilva in https://github.com/laravel/framework/pull/48845 - [10.x] Fix the replace() method in DefaultService class by @jonagoldman in https://github.com/laravel/framework/pull/49022
- [10.x] Pass the property $validator as a parameter to the $callback Closure by @shinsenter in https://github.com/laravel/framework/pull/49015
- [10.x] Fix Cache DatabaseStore::add() error occur on Postgres within transaction by @xdevor in https://github.com/laravel/framework/pull/49025
- [10.x] Support asserting against chained batches by @taylorotwell in https://github.com/laravel/framework/pull/49003
- [10.x] Prevent DB
Cache::get()
occur race condition by @xdevor in https://github.com/laravel/framework/pull/49031 - [10.x] Fix notifications being counted as sent without a "shouldSend" method by @joelwmale in https://github.com/laravel/framework/pull/49030
- [10.x] Fix tests failure on Windows by @hafezdivandari in https://github.com/laravel/framework/pull/49037
- [10.x] Add unless conditional on validation rules by @michaelnabil230 in https://github.com/laravel/framework/pull/49048
- [10.x] Handle string based payloads that are not JSON or form data when creating PSR request instances by @timacdonald in https://github.com/laravel/framework/pull/49047
- [10.x] Fix directory separator CMD display on windows by @imanghafoori1 in https://github.com/laravel/framework/pull/49045
- [10.x] Fix mapSpread doc by @timacdonald in https://github.com/laravel/framework/pull/48941
- [10.x] Tiny
Support\Collection
test fix - Unused data provider parameter by @stevebauman in https://github.com/laravel/framework/pull/49053 - [10.x] Feat: Add color_hex validation rule by @nikopeikrishvili in https://github.com/laravel/framework/pull/49056
- [10.x] Handle missing translation strings using callback by @DeanWunder in https://github.com/laravel/framework/pull/49040
- [10.x] Add Str::transliterate to Stringable by @dwightwatson in https://github.com/laravel/framework/pull/49065
- Add Alpha Channel support to Hex validation rule by @ahinkle in https://github.com/laravel/framework/pull/49069