编程

model:show 查看模型中的第三方关联:Laravel 11.11 中的新功能

234 2024-06-23 18:19:00

Laravel 团队发布了 v11.11,支持使用 model:show 命令显示第三方关联,新增 Collection 方法,新的缓存事件等等。

before 及 after Collection 方法

Ryuta Hamasaki 方法贡献 beforeafter 方法到 CollectionLazyCollection 实例

以下是 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 值做出了贡献。现在,你可以通过其他重要的环境信息快速确定配置的区域设置和时区:

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 SemanticsPull 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