model:show 查看模型中的第三方关联:Laravel 11.11 中的新功能
Laravel 团队发布了 v11.11,支持使用 model:show
命令显示第三方关联,新增 Collection 方法,新的缓存事件等等。
before 及 after Collection 方法
Ryuta Hamasaki 方法贡献 before 和 after 方法到 Collection 和 LazyCollection 实例
以下是 before
方法的示例:
$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
以下是 after
方法的示例:
$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 贡献了新的缓存事件,应用可以监听此新增事件:
use Illuminate\Cache\Events\ForgettingKey;
use Illuminate\Cache\Events\KeyForgetFailed;
use Illuminate\Cache\Events\KeyWriteFailed;
// Two public properties: `$this->value` and `$this->seconds`
use Illuminate\Cache\Events\RetrievingKey;
use Illuminate\Cache\Events\RetrievingManyKeys;
// One public property: `$this->keys`
use Illuminate\Cache\Events\WritingKey
// Two public properties: `$this->value` and `$this->seconds`
use Illuminate\Cache\Events\WritingManyKeys;
// Three public properties:
// `$this->keys`, `$this->values` and `$this->seconds`
Support Third-party Relations in model:show
Jonas Staudenmeir 贡献了在 model:show
命令中引入了第三方包的模型关联的能力:
通过分析方法中的代码,并查找 $this->hasMany( 等代码,
model:show
命令可查找模型的关联。但它不能检测第三方关联(如 eloquent-has-many-deep)。我们可以构建一个完整的系统来连接包,但这太过头了。相反,我们可以检查方法的返回类型,看看它是否是基类 Relation 的子类。不是每个人都使用返回类型,但我希望现在大多数人都这样做。
更多详情查看 Pull Request #51807.
Session ID Getter
Tim MacDonald 贡献了 Session::id() 方法到 Session facade 中:
Session::id();
// You can still use `getId()` too:
Session::getId();
时区和区域设置添加到 about 命令
Amir Khalife Soltani 为 Laravel 的 abou t命令添加 Locale 和 Timezone 值做出了贡献。现在,你可以通过其他重要的环境信息快速确定配置的区域设置和时区:
![](https://www.tubring.cn/storage/articles/HrLII4DmF7bYKDLfgqzCTfIvSXWpt1Mm1wSBJWfj.png)
422 不可处理的内容状态代码(Unprocessable Content Status Code)
Dwight Watson 贡献了对用于确定 422 状态代码的方法更新,该 422 状态代码现在被称为 422 不可处理内容(422 Unprocessable Content)而不是不可处理实体(Unprocessable Entity)。断言 422 状态代码的交互方式没有改变:
$response = $this->postJson('/example', []);
$response->assertUnprocessable();
详情请查看 RFC 9110: HTTP Semantics 和 Pull Request #51815。
添加了 Relation::getMorphAlias() 方法
Dennis Koch 贡献了 Relation::getMorphAlias() 方法
因为存在 Relation::getMorphedModel() 方法, 我认为添加反向的 getMorphAlias 是有益的。
这个增加对测试特别有用,因为它简化了 $this->assertDatabaseHas 断言。开发者不再需要调用所使用的 morph 别名
$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 Renderer\Exception 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 \Illuminate\Http\Response by @seriquynh in https://github.com/laravel/framework/pull/51823