事务事件捕获
你大概已经习惯用 DB::transaction() 来保证数据一致性,对吧?Laravel 现在在事务工具箱里塞进了一个新帮手 afterRollback()。它会在事务失败时自动触发,让你不需要额外写 try-catch 就能处理清理、记录日志或发送通知等动作。
use Illuminate\Support\Facades\DB;
DB::transaction(function () {
DB::afterCommit(function () {
// 事务提交成功时执行
});
DB::afterRollback(function () {
// 事务回滚时执行
});
// 你的事务性代码...
});
Request Batching:一口气处理多个 HTTP 请求
一次性发起多个 HTTP 请求,现在变得更简洁了。借助全新的 Request Batching,你可以优雅地收集多个调用并统一发送,同时在生命周期的不同阶段加入回调。
$responses = Http::batch(fn (Batch $b) => [
$b->as('users')->get('https://api.example.local/users'),
$b->as('orders')->get('https://api.example.local/orders'),
])->send();
$users = $responses['users']->json();
Dynamic Wheres:更优雅的条件查询
一个不大却格外顺手的改进:Eloquent 现在可以通过 动态 where 方法 来组合条件。过去你可能这么写:
// 以前
$order = Order::where('invoice', '123')->where('status', 'pending')->first();
// 现在
$order = Order::whereInvoiceAndStatus('123', 'pending')->first();