The Laravel crew launched 9.40 this week with a brand new lottery utility class, new validation guidelines, asserting a redirect to a named route, and extra.
Lottery utility class
Tim MacDonald contributed a Lottery
help class that can be utilized in varied settings, equivalent to randomizing information sampling, failure stories, and so forth.
This is an instance of the essential API:
1Lottery::odds(1, 10)->select();
2Â
3// returns `true` for "win"
4// returns `false` for "loss"
5Â
6Lottery::odds(1, 10)
7 ->winner(fn () => 'winner')
8 ->loser(fn () => 'loser')
9 ->select();
10Â
11// returns `"winner"` for "win"
12// returns `"loser"` for "loss"
Listed below are a couple of Laravel-specific examples from the pull request, which is the place Lottery could possibly be utilized in tandem with different Laravel options. Within the following instance, the Lottery
chain of calls returns a callable
occasion which you’ll be able to register as a handler in varied Laravel occasions:
1// Randomly pattern so we do not flood our error handler...
2Â
3DB::whenQueryingForLongerThan(
4 Interval::seconds(2),
5 Lottery::odds(1, 100)->winner(fn () => report('DB queries exceeded 2 seconds'))
6);
7Â
8// Randomly pattern so we do not flood our error handler...
9Â
10Mannequin::handleLazyLoadingViolationUsing(Lottery::odds(1, 5000)->winner(operate ($mannequin, $relation) {
11 report(new LazyLoadingViolationException($mannequin, $relation));
12}));
The Laravel helpers documentation has examples of the right way to use this class and the right way to take a look at it as nicely.
Eloquent mannequin observers listed within the mannequin:present command
Mike Healy contributed an replace to the mannequin:present
command that lists mannequin observers for a given mannequin:
It additionally handles a number of observers connected to the identical mannequin motion. Itemizing observers is useful to rapidly discover listed observers for a mannequin with out trying to find service supplier calls.
Lowercase and uppercase validation guidelines
Tim MacDonald contributed a lowercase
validation rule, which enforces that the validated enter is lowercase. This could possibly be helpful when you do not wish to change data on the person to lowercase silently, however you wish to guarantee they insert solely lowercase. For instance, an enter to create a database person however forcing the person to be all lowercase.
1Validator::make(
2 ['name' => 'Admin'],
3 ['name' => 'required|string|lowercase']
4);
Michael Nabil contributed an uppercase
validation rule which ensures the given enter is in all uppercase letters:
1Validator::make(
2 ['name' => 'ADMIN'],
3 ['name' => ['required', 'string', 'uppercase']
4);
Assert redirect to a route
Zaher Ghaibeh contributed an assertRedirectToRoute
methodology which you need to use to claim {that a} response is a redirect to a given named route:
1this->get('test-route')
2 ->assertRedirectToRoute('named-route');
Save many fashions quietly
Niels contributed a saveManyQuietly
methodology for saving a number of fashions quietly for belongs to many and has one or many relationships.
1$mannequin->saveManyQuietly($relatedModels);
Launch Notes
You’ll be able to see the whole checklist of latest options and updates beneath and the diff between 9.39.0 and 9.40.0 on GitHub. The next launch notes are instantly from the changelog:
v9.40.0
Added
- Embrace Eloquent Mannequin Observers in mannequin:present command (#44884)
- Added “lowercase” validation rule (#44883)
- Introduce
Lottery
class (#44894) - Added
/Illuminate/Testing/TestResponse::assertRedirectToRoute()
(#44926) - Add uppercase validation rule (#44918)
- Added saveManyQuietly to the hasOneOrMany and belongsToMany relations (#44913)
Fastened
- Repair HasAttributes::getMutatedAttributes for lessons with constructor args (#44829)
Modified
- Take away argument task for console (#44888)
- Move $maxExceptions from mailable to underlying job when queuing (#44903)
- Make Vite::isRunningHot public (#44900)
- Add methodology to have the ability to override the exception context format (#44895)
- Add zero-width house to trimmed characters in TrimStrings middleware (#44906)
- Present error if key:generate artisan command fails (#44927)
- Replace database model examine for lock popping for PlanetScale (#44925)
- Transfer operate withoutTrashed into DatabaseRule (#44938)
- Use write connection on Schema::getColumnListing() and Schema::hasTable() for MySQL and PostgreSQL (#44946)