Back to Blog

Laravel 9.2 is Released

Julian Beaujardin
Julian Beaujardin Reference: Laravel News February 25th, 2022

The Laravel team released version 9.2.0 with an array keyBy method, an Eloquent attribute static constructor, moving the Laravel CORS package into the framework, and more:

Attribute Make Method

@ARI contributed a static constructor method to the Eloquent Attribute class, which provides convenience as follows:

// Using the new keyword
return (new Attribute(
    get: fn ($value) => strtoupper($value),
    set: fn ($value) => strtoupper($value)
))->withoutObjectCaching();

// The new make() static constructor method
return Attribute::make(
    get: fn ($value) => strtoupper($value),
    set: fn ($value) => strtoupper($value)
)->withoutObjectCaching();

Array keyBy Method

Douglas Medeiros contributed an Arr::keyBy() method which works like the collection keyBy() method:

$array = [
    ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
    ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
];

Arr::keyBy($array, 'id');
/*
[
    '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
    // The second element of an original array is overwritten by the last element because of the same id
]
*/

Expects Output to Contain Test Assertion

Francisco Madeira contributed a expectsOutputToContain test method to assert that artisan commands contain a substring of output:

$this->artisan('Hello World')
     ->expectsOutputToContain('Hello');

Add X headers when using Mail::alwaysTo

Craig Morris contributed adding X headers in development when using the Mail::alwaysTo() method:

When using Mail::alwaysTo in development environments, the original To, Cc and Bcc are lost. This makes it difficult to determine where the email was going to during testing. This PR adds the original to, cc and bcc into X-Headers in the email so this information can be retrieved, while still preventing the email being sent to these recipients.

This is useful for debugging the intended to, cc, and bcc fields, yet only sending email to the specified alwaysTo address. Check out Pull Request #41101 for details.

Integrate Laravel CORS into framework

Dries Vints migrated the fruitcake/laravel-cors package into the Laravel framework:

The main reason is that we want to remove a circular dependency we rely on additionally to the fact that we eliminate another dependency of the skeleton.

All credits for the code go to @barryvdh of @fruitcake . Thanks for maintaining that package for so long!

String "Between First" Method

Yoeri Boven contributed a betweenFirst() method which gets the smallest possible portion of a string between two given values:

Str::betweenFirst('[a]ab[b]', '[', ']'); // a
Str::betweenFirst('foofoobar', 'foo', 'bar'); // foo
Str::betweenFirst('hannah', 'ha', 'ah'); // nn
Str::betweenFirst('dddabcddd', 'a', 'c')); // b

Allow Specifying a Custom Message for Rule Objects

Ryan Chandler contributed a way to specify a custom error message when validating with a Rule object. With this update, you can provide a custom message to the messages array:

$request->validate(
    [
        'foo' => [new Example]
    ],
    [
        Example::class => 'My custom message goes here!'
    ]
);

Release Notes

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

Added

  • Added Illuminate/Database/Eloquent/Casts/Attribute::make() (#41014)
  • Added Illuminate/Collections/Arr::keyBy() (#41029)
  • Added expectsOutputToContain to the PendingCommand. (#40984)
  • Added ability to supply HTTP client methods with JsonSerializable instances (#41055)
  • Added Illuminate/Filesystem/AwsS3V3Adapter::getClinet() (#41079)
  • Added Support for enum in Builder::whereRelation (#41091)
  • Added X headers when using Mail::alwaysTo (#41101)
  • Added of support Bitwise operators in query (#41112)
  • Integrate Laravel CORS into framework (#41137)
  • Added Illuminate/Support/Str::betweenFirst() (#41144)
  • Allow specifiying custom messages for Rule objects (#41145)

Fixed

  • Fixed Queue Failed_jobs insert issue with Exception contain UNICODE (#41020)
  • Fixes attempt to log deprecations on mocks (#41057)
  • Fixed loadAggregate not correctly applying casts (#41050)
  • Do not transform JsonSerializable instances to array in HTTP client methods (#41077)
  • Fix parsing config('database.connections.pgsql.search_path') (#41088)
  • Eloquent: firstWhere returns Object instead of NULL (#41099)
  • Fixed updated with provided qualified updated_at (#41133)
  • Fix setPriority Call for MailChannel (#41120)
  • Fixed route:list command output (#41177)
  • Fix database migrations $connection property (#41161)

Changed

  • Cursor pagination: convert original column to expression (#41003)
  • Cast $perPage to integer on Paginator (#41073)
  • Restore S3 client extra options (#41097)
  • Use latest() within notifications() in Illuminate/Notifications/HasDatabaseNotifications.php (#41095)
  • Remove duplicate queries to find batch (#41121)
  • Remove redundant check in FormRequest::validated() (#41115)
  • Illuminate/Support/Facades/Storage::fake() changed (#41113)
  • Use coalesce equal as provided by PHP >= 7.4 (#41174)
  • Simplify some conditions with is_countable() (#41168)
  • Pass AWS temporary URL options to createPresignedRequest method (#41156)
  • Let Multiple* exceptions hold the number of records and items found (#41164)