This week, the Laravel team released v11.11, with support for third-party relations in the model:show command, new Collection methods, new cache events, and more.
before and after Collection Methods
Ryuta Hamasaki contributed before and after methods to Collection and LazyCollection instances
Here are examples of the before method from the pull request description:
$collection = collect([1, 2, 3, 4, 5, ‘name’ => ‘taylor’, ‘framework’ => ‘laravel’]);
$collection->before(2) // 1
$collection->before(‘taylor’) // 5
$collection->before(‘laravel’) // ‘taylor’
$collection->before(fn ($value) => $value > 4) // 4
$collection->before(fn ($value) => ! is_numeric($value)) // 5
$collection->before(1) // null
$collection->before(‘not found’) // null
Here are examples of the after method from the pull request description:
$collection = collect([1, 2, 3, 4, 5, ‘name’ => ‘taylor’, ‘framework’ => ‘laravel’]);
$collection->after(1) // 2
$collection->after(‘taylor’) // ‘laravel’
$collection->after(fn ($value) => $value > 4) // ‘taylor’
$collection->after(fn ($value) => ! is_numeric($value)) // ‘laravel’
$collection->after(‘laravel’) // null
$collection->after(‘not found’) // null
Cache Events
Alex Bouma contributed new cache events to the framework that applications can listen to:
use IlluminateCacheEventsForgettingKey;
use IlluminateCacheEventsKeyForgetFailed;
use IlluminateCacheEventsKeyWriteFailed;
// Two public properties: `$this->value` and `$this->seconds`
use IlluminateCacheEventsRetrievingKey;
use IlluminateCacheEventsRetrievingManyKeys;
// One public property: `$this->keys`
use IlluminateCacheEventsWritingKey
// Two public properties: `$this->value` and `$this->seconds`
use IlluminateCacheEventsWritingManyKeys;
// Three public properties:
// `$this->keys`, `$this->values` and `$this->seconds`
Support Third-party Relations in model:show
Jonas Staudenmeir contributed the ability to include third-party package model relations in the model:show command:
The model:show command finds a model’s relations by analyzing the code of its methods and looking for $this->hasMany( etc. This doesn’t detect third-party relations (like eloquent-has-many-deep). We could build a whole system for packages to hook into, but that would be overkill, IMO.
Instead, we can check the method’s return type and see if it’s a subclass of the base Relation class. Not everybody uses return types, but I hope that most do nowadays.
You can learn more about this addition in Pull Request #51807.
Session ID Getter
Tim MacDonald contributed the Session::id() method to the Session facade:
Session::id();
// You can still use `getId()` too:
Session::getId();
Timezone and Locale Added to the about Command
Amir Khalife Soltani contributed to adding Locale and Timezone values to Laravel’s about command. You can now quickly determine what the configured locale and timezone are at a glance with other important environment information:
The `about` command with `Timezone` and `Locale` values
422 Unprocessable Content Status Code
Dwight Watson contributed updates to the status code method for determining a 422 status code, which is now referred to as 422 Unprocessable Content instead of Unprocessable Entity. The way you interact with asserting a 422 status code hasn’t changed:
$response = $this->postJson(‘/example’, []);
$response->assertUnprocessable();
See RFC 9110: HTTP Semantics and Pull Request #51815 for details.
Add Relation::getMorphAlias() Method
Dennis Koch contributed a Relation::getMorphAlias() method
Since there is a Relation::getMorphedModel() I think it would be beneficial to add the reverse getMorphAlias.
This addition is particularly beneficial for testing, as it simplifies assertions with $this->assertDatabaseHas. Developers no longer need to recall the morph alias used:
$this->assertDatabaseHas(‘taskables’, [
‘taskable_type’ => Relation::getMorphAlias(Document::class),
‘taskable_id’ => $mitigation->id,
‘task_id’ => $taskB->id
]);
Release notes
You can see the complete list of new features and updates below and the diff between 11.10.0 and 11.11.0 on GitHub. The following release notes are directly from the changelog:
v11.11.0
[11.x] Add get, write and forget cache events by @stayallive in https://github.com/laravel/framework/pull/51560
[11.x] Add test for Arr::sortRecursiveDesc() method. by @lmottasin in https://github.com/laravel/framework/pull/51716
[11.x] Fix missing table name in db:table command by @benholmen in https://github.com/laravel/framework/pull/51710
Ensure files exist for install:broadcasting by @jasonmccreary in https://github.com/laravel/framework/pull/51719
[11.x] Restore exceptions/errors to test assertion failure messages by @jessarcher in https://github.com/laravel/framework/pull/51725
[11.x] Test Improvements by @crynobone in https://github.com/laravel/framework/pull/51723
[11.x] Add tests for accessible and take method by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51724
Increment the totalJobs property for the BatchFake when add some jobs by @yankewei in https://github.com/laravel/framework/pull/51742
[11.x] Give session ID retrieval the Laravel treatment by @timacdonald in https://github.com/laravel/framework/pull/51732
[11.x] Fix the chunk method to an integer type in the splitIn method by @rookiexxk in https://github.com/laravel/framework/pull/51733
Update:update name method and doc by @mehdi-fathi in https://github.com/laravel/framework/pull/51744
[11.x] Fixes config:publish with dontMergeFrameworkConfiguration() set to true by @crynobone in https://github.com/laravel/framework/pull/51751
Updated phpdoc for Builder::from() by @boris-glumpler in https://github.com/laravel/framework/pull/51767
[11.x] Fixed pop on default Beankstalkd queue when not specifically added by @rinocs in https://github.com/laravel/framework/pull/51759
[11.x] Add before and after methods to Collection by @avosalmon in https://github.com/laravel/framework/pull/51752
[11.x] Change scope for afterCreating and afterMaking callbacks by @jacob418 in https://github.com/laravel/framework/pull/51772
Use numeric literal separator in file rule validation by @AmirKhalifehSoltani in https://github.com/laravel/framework/pull/51781
[11.x] Import Model class for RendererException by @seriquynh in https://github.com/laravel/framework/pull/51778
[11.x] About command improvement by @AmirKhalifehSoltani in https://github.com/laravel/framework/pull/51791
[11.x] Test abort behavior by @seriquynh in https://github.com/laravel/framework/pull/51800
[11.x] Container shares fixed values/initialized instances instead of singleton closure resolutions by @seriquynh in https://github.com/laravel/framework/pull/51804
[11.x] Fix altering a table that has a column with default 0 on SQLite by @hafezdivandari in https://github.com/laravel/framework/pull/51803
[11.x] Fix typo in VendorPublishCommand by @tamiroh in https://github.com/laravel/framework/pull/51812
[11.x] Fix some typos in the tests by @tamiroh in https://github.com/laravel/framework/pull/51811
[11.x] Add unprocessableContent and update unprocessableEntity by @dwightwatson in https://github.com/laravel/framework/pull/51815
[11.x] Improve Queue::assertNothingPushed() error message by @SjorsO in https://github.com/laravel/framework/pull/51814
[11.x] Add Relation::getMorphAlias() by @pxlrbt in https://github.com/laravel/framework/pull/51809
[11.x] Support third-party relations in model:show command by @staudenmeir in https://github.com/laravel/framework/pull/51807
[11.x] Fix nested rules custom attribute names by @owenandrews in https://github.com/laravel/framework/pull/51805
[11.x] Fix docblock of IlluminateHttpResponse by @seriquynh in https://github.com/laravel/framework/pull/51823
The post View Third-party Relations in model:show – Now Available in Laravel 11.11 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Source: Read MoreÂ