Laravel 10.19 Released
Published on by Paul Redmond
This week, the Laravel team released v10.19 with a collection percentage method, custom event discovery class resolution, dynamic queue listener delay, and more:
Word wrap string method
Josh Bonnick contributed a wordWrap
method to the Laravel String API, which splits strings within a string using a character limit. This method is a wrapper for PHP's wordwrap function:
use Illuminate\Support\Str; $text = "A very long woooooooooooord." // StrStr::wordWrap(string: $text, characters: 8);/*A verylongwoooooooooooord.*/ // Stringablestr($text)->wordWrap(string: $text, characters: 8);
Add percentage method to collections
Wendell Adriel contributed a percentage()
method to collections that allows you to calculate a percentage based on custom logic using a truth
test:
$collection = new $collection([ ['name' => 'Taylor', 'foo' => 'foo'], ['name' => 'Nuno', 'foo' => 'bar'], ['name' => 'Dries', 'foo' => 'bar'], ['name' => 'Jess', 'foo' => 'baz'],]); $collection->percentage(fn ($value) => $value['foo'] === 'foo'); // 25.00$collection->percentage(fn ($value) => $value['foo'] === 'bar'); // 50.00$collection->percentage(fn ($value) => $value['foo'] === 'baz'); // 25.00$collection->percentage(fn ($value) => $value['foo'] === 'test'); // 0.00
Ability to customize class resolution in Event discovery
@bastien-phi contributed the ability to customize class resolution for Event discovery:
This PR adds the ability to customize the way the translation is done and unlocks event discovery in such DDD structures.
Here's an example of a custom callback in Pull Request #48031's tests, which illustrates how this can be used:
use Illuminate\Foundation\Events\DiscoverEvents; DiscoverEvents::guessClassNamesUsing(function (SplFileInfo $file, $basePath) { return Str::of($file->getRealPath()) ->after($basePath.DIRECTORY_SEPARATOR) ->before('.php') ->replace(DIRECTORY_SEPARATOR, '\\') ->ucfirst() ->prepend('Illuminate\\') ->toString();});
Specify listener delay dynamically
@CalebW contributed support for listeners to define a withDelay()
method that can determine queue listener delay:
class Listener implements ShouldQueue{ public function __invoke(Event $event): void { // ... } public function withDelay(Event $event): int { return $event->fooBar ? <short_delay> : <long_delay>; }}
Add dynamic return types to the rescue helper
Choraimy Kroonstuiver contributed a PHPDoc update to help static analysis tools understand the generic return types of the rescue()
helper. There are no code examples to see here, but the updated Docblock looks as follows after this update:
/** * Catch a potential exception and return a default value. * * @template TRescueValue * @template TRescueFallback * * @param callable(): TRescueValue $callback * @param (callable(\Throwable): TRescueFallback)|TRescueFallback $rescue * @param bool|callable $report * @return TRescueValue|TRescueFallback */function rescue(callable $callback, $rescue = null, $report = true){ // ...} // Examples: assertType('int|null', rescue(fn () => 123));assertType('int', rescue(fn () => 123, 345));assertType('int', rescue(fn () => 123, fn () => 345));
You can learn more about Generics By Examples on the PHPStan blog.
Add count argument to createMany and createManyQuietly
Jordan Welch contributed the ability to pass an integer to createMany()
and createManyQuietly()
on model factories to create that number of models:
// Before:$users = User::factory()->createMany([[], [], []]); // After:$users = User::factory()->createMany(3); $this->assertCount(3, $users);$this->assertInstanceOf(Eloquent\Collection::class, $users);
Release notes
You can see the complete list of new features and updates below and the diff between 10.18.0 and 10.19.0 on GitHub. The following release notes are directly from the changelog:
v10.19.0
- [10.x] Fix typo in update
HasUniqueIds
by @iamcarlos94 in https://github.com/laravel/framework/pull/47994 - [10.x] Gracefully handle scientific notation by @timacdonald in https://github.com/laravel/framework/pull/48002
- [10.x] Fix docblocks for throw_if and throw_unless by @AbdelElrafa in https://github.com/laravel/framework/pull/48003
- [10.x] Add
wordWrap
toStr
by @joshbonnick in https://github.com/laravel/framework/pull/48012 - [10.x] Fix RetryBatchCommand overlapping of failed jobs when run concurrently with the same Batch ID using isolatableId by @rybakihor in https://github.com/laravel/framework/pull/48000
- [10.x] Fix
assertRedirectToRoute
when route uri is empty by @khernik93 in https://github.com/laravel/framework/pull/48023 - [10.x] Fix empty table displayed when using the --pending option but there are no pending migrations by @TheBlckbird in https://github.com/laravel/framework/pull/48019
- [10.x] Fix forced use of write DB connection by @oleksiikhr in https://github.com/laravel/framework/pull/48015
- [10.x] Use model cast when builder created updated at value by @timacdonald in https://github.com/laravel/framework/pull/47942
- [10.x] Fix Collection::search and LazyCollection::search return type by @bastien-phi in https://github.com/laravel/framework/pull/48030
- [10.x] Add ability to customize class resolution in event discovery by @bastien-phi in https://github.com/laravel/framework/pull/48031
- [10.x] Add
percentage
method to Collections by @WendellAdriel in https://github.com/laravel/framework/pull/48034 - [10.x] Fix parsing error in console when parameter description contains
--
by @rxrw in https://github.com/laravel/framework/pull/48021 - [10.x] Allow Listeners to dynamically specify delay using
withDelay
by @CalebDW in https://github.com/laravel/framework/pull/48026 - [10.x] Add dynamic return types to rescue helper by @axlon in https://github.com/laravel/framework/pull/48062
- [10.x] createMany & createManyQuietly add count argument by @JHWelch in https://github.com/laravel/framework/pull/48048
- [10.x] Attributes support on default component slot by @royduin in https://github.com/laravel/framework/pull/48039
- [10.x] Add WithoutRelations attribute for model serialization by @Neol3108 in https://github.com/laravel/framework/pull/47989
- [10.x] Can apply WithoutRelations to entire class by @cosmastech in https://github.com/laravel/framework/pull/48068
- [10.x] createMany & createManyQuietly make argument optional by @JHWelch in https://github.com/laravel/framework/pull/48070