Laravel 9.40 Released


November 16th, 2022

Laravel 9.40 Released

The Laravel team released 9.40 this week with a new lottery utility class, new validation rules, asserting a redirect to a named route, and more.

Lottery utility class

Tim MacDonald contributed a Lottery support class that can be used in various settings, such as randomizing data sampling, failure reports, etc.

Here's an example of the basic API:

1Lottery::odds(1, 10)->choose();
3// returns `true` for "win"
4// returns `false` for "loss"
6Lottery::odds(1, 10)
7 ->winner(fn () => 'winner')
8 ->loser(fn () => 'loser')
9 ->choose();
11// returns `"winner"` for "win"
12// returns `"loser"` for "loss"

Here are a few Laravel-specific examples from the pull request, which is where Lottery could be used in tandem with other Laravel features. In the following example, the Lottery chain of calls returns a callable instance which you can register as a handler in various Laravel events:

1// Randomly sample so we don't flood our error handler...
4 Interval::seconds(2),
5 Lottery::odds(1, 100)->winner(fn () => report('DB queries exceeded 2 seconds'))
8// Randomly sample so we don't flood our error handler...
10Model::handleLazyLoadingViolationUsing(Lottery::odds(1, 5000)->winner(function ($model, $relation) {
11 report(new LazyLoadingViolationException($model, $relation));

The Laravel helpers documentation has examples of how to use this class and how to test it as well.

Eloquent model observers listed in the model:show command

Mike Healy contributed an update to the model:show command that lists model observers for a given model:

model:show observers example

It also handles multiple observers attached to the same model action. Listing observers is helpful to quickly find listed observers for a model without hunting for service provider calls.

Lowercase and uppercase validation rules

Tim MacDonald contributed a lowercase validation rule, which enforces that the validated input is lowercase. This could be useful when you don't want to change info on the user to lowercase silently, but you want to ensure they insert only lowercase. For example, an input to create a database user but forcing the user to be all lowercase.

2 ['name' => 'Admin'],
3 ['name' => 'required|string|lowercase']

Michael Nabil contributed an uppercase validation rule which ensures the given input is in all uppercase letters:

2 ['name' => 'ADMIN'],
3 ['name' => ['required', 'string', 'uppercase']

Assert redirect to a route

Zaher Ghaibeh contributed an assertRedirectToRoute method which you can use to assert that a response is a redirect to a given named route:

2 ->assertRedirectToRoute('named-route');

Save many models quietly

Niels contributed a saveManyQuietly method for saving multiple models quietly for belongs to many and has one or many relationships.


Release Notes

You can see the complete list of new features and updates below and the diff between 9.39.0 and 9.40.0 on GitHub. The following release notes are directly from the changelog:



  • Include Eloquent Model Observers in model:show 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)


  • Fix HasAttributes::getMutatedAttributes for classes with constructor args (#44829)


  • Remove argument assignment for console (#44888)
  • Pass $maxExceptions from mailable to underlying job when queuing (#44903)
  • Make Vite::isRunningHot public (#44900)
  • Add method to be able to override the exception context format (#44895)
  • Add zero-width space to trimmed characters in TrimStrings middleware (#44906)
  • Show error if key:generate artisan command fails (#44927)
  • Update database version check for lock popping for PlanetScale (#44925)
  • Move function withoutTrashed into DatabaseRule (#44938)
  • Use write connection on Schema::getColumnListing() and Schema::hasTable() for MySQL and PostgreSQL (#44946)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.