Laravel 9.40 Released

Published on by

Laravel 9.40 Released image

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:

Lottery::odds(1, 10)->choose();
 
// returns `true` for "win"
// returns `false` for "loss"
 
Lottery::odds(1, 10)
->winner(fn () => 'winner')
->loser(fn () => 'loser')
->choose();
 
// returns `"winner"` for "win"
// 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:

// Randomly sample so we don't flood our error handler...
 
DB::whenQueryingForLongerThan(
Interval::seconds(2),
Lottery::odds(1, 100)->winner(fn () => report('DB queries exceeded 2 seconds'))
);
 
// Randomly sample so we don't flood our error handler...
 
Model::handleLazyLoadingViolationUsing(Lottery::odds(1, 5000)->winner(function ($model, $relation) {
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:

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.

Validator::make(
['name' => 'Admin'],
['name' => 'required|string|lowercase']
);

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

Validator::make(
['name' => 'ADMIN'],
['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:

this->get('test-route')
->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.

$model->saveManyQuietly($relatedModels);

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:

v9.40.0

Added

  • 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)

Fixed

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

Changed

  • 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)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8 image

A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8

Read article
New Proposed Array Find Functions in PHP 8.4 image

New Proposed Array Find Functions in PHP 8.4

Read article
Laracon AU 2024 tickets are now on sale image

Laracon AU 2024 tickets are now on sale

Read article
Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris image

Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris

Read article
Is class instantiation without extra parenthesis coming to PHP 8.4? image

Is class instantiation without extra parenthesis coming to PHP 8.4?

Read article
Validation Errors Card for Laravel Pulse image

Validation Errors Card for Laravel Pulse

Read article