Laravel 9.31 Released

News

September 21st, 2022

Laravel 9.31 Released

The Laravel team released 9.31 with a request lifecycle duration handler, update model without changing timestamps, fake batches for testing, and more:

Request lifecycle duration handler

Tim MacDonald contributed a callback handler when a request lifecycle is longer than a given time limit:

1use Carbon\CarbonInterval as Interval;
2use Illuminate\Contracts\Http\Kernel;
3 
4public function boot()
5{
6 if ($this->app->runningInConsole()) {
7 return;
8 }
9 
10 $kernel = $this->app[Kernel::class];
11 $kernel->whenRequestLifecycleIsLongerThan(
12 Interval::seconds(1),
13 fn ($startedAt, $request, $response) => /* ... */
14 );
15}

This callback is similar to the cumulative database query time released in Laravel 9.18 Released, but for the request lifecycle.

Model "without timestamps" feature

Tim MacDonald contributed a static withoutTimestamps() method where updated_at will not change:

1$user = User::first();
2 
3 
4// `updated_at` is not changed...
5 
6User::withoutTimestamps(
7 fn () => $user->update(['reserved_at' => now()])
8);

Vite manifestHash function

Enzo Innocenzi contributed a manifestHash() method to the Vite class, that returns a unique hash if the manifest exists. This can be used to invalidate assets. See Pull Request #44136 for further details.

Fake batches

Taylor Otwell contributed fake batches:

It's currently hard to test things like if a batch was cancelled by a job or if a job added additional jobs to a batch. You have to create a FakeBatch manually and override the cancel / add methods, etc.

This solves that.

1[$job, $batch] = (new TestJob)->withFakeBatch();
2 
3$job->handle();
4 
5$this->assertTrue($batch->cancelled());
6$this->assertNotEmpty($batch->added);

Model getAppends() method

Arturo Rodríguez added an accessor method to Model to get the accessors that are being appended to model arrays. This could be useful for custom model mappings:

1$model->getAppends();

Str wrap static method

Steve Bauman added a missing static Str::wrap() method, which was only available via Stringable:

1Str::wrap('-bar-', 'foo', 'baz'); // 'foo-bar-baz'
2str('-bar-')->wrap('foo', 'baz'); // 'foo-bar-baz'

Macroable vite

Tim MacDonald contributed the Macroable trait to Illuminate\Foundation\Vite to create aliases matching your JS config:

1Vite::macro('image', fn ($asset) => $this->asset("resources/images/{$asset}"));
2 
3// Usage: <img src="{{ Vite::image('profile.png') }}" ... >

See Pull Request #44198 for further details.

Release Notes

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

v9.31.0

Added

  • Added unique deferrable initially deferred constants for PostgreSQL (#44127)
  • Request lifecycle duration handler (#44122)
  • Added Model::withoutTimestamps(...) (#44138)
  • Added manifestHash function to Illuminate\Foundation\Vite (#44136)
  • Added support for operator <=> in /Illuminate/Collections/Traits/EnumeratesValues::operatorForWhere() (#44154)
  • Added that Illuminate/Database/Connection::registerDoctrineType() can accept object as well as classname for new doctrine type (#44149)
  • Added Fake Batches (#44104, #44173)
  • Added Model::getAppends() (#44180)
  • Added missing Str::wrap() static method (#44207)
  • Added require symfony/uid (#44202)
  • Make Vite macroable (#44198)

Fixed

  • Async fix in Illuminate/Http/Client/PendingRequest (#44179)
  • Fixes artisan serve command with PHP_CLI_SERVER_WORKERS environment variable (#44204)
  • Fixed InteractsWithDatabase::castAsJson($value) incorrectly handles SQLite Database (#44196)

Changed

  • Improve Blade compilation exception messages (#44134)
  • Improve test failure output (#43943)
  • Prompt to create MySQL db when migrating (#44153)
  • Improve UUID and ULID support for Eloquent (#44146)

Filed in:

Paul Redmond

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