Laravel 6.x 于 2019 年 9 月发布,作为长期支持版本(LTS),其核心特性包括:
1. 语义化版本控制
采用 major.minor.patch 命名规范(如 6.0.0),明确版本迭代规则。
2. 任务中间件(Job Middleware)
允许在队列任务执行前后插入逻辑,例如:
php
// 定义中间件
class LogJobMiddleware {
public function handle($job, $next) {
Log::info('Job starting: '.get_class($job));
$next($job);
}
}
// 在任务中调用
class ProcessOrder implements ShouldQueue {
public function middleware() {
return [new LogJobMiddleware];
}
}
3. Laravel UI 分离
前端脚手架(如 auth 认证模板)被拆分为独立包 laravel/ui:
bash
composer require laravel/ui
php artisan ui vue --auth
4. Eloquent 子查询增强
支持在查询构建器中嵌套子查询:
php
// 获取每个用户的最新订单
User::select()->addSelect([
'last_order' => Order::select('created_at')
->whereColumn('user_id', 'users.id')
->latest()
->limit(1)
])->get();
5. 惰性集合(Lazy Collections)
处理大数据集时减少内存占用:
php
use Illuminate\Support\LazyCollection;
LazyCollection::make(function () {
$handle = fopen('large.log', 'r');
while ($line = fgets($handle)) yield $line;
})->chunk(1000)->each(function ($lines) {
// 分批处理
});
6. 其他更新
- 路由模型绑定优化 :支持自定义键名(如
Route::get('posts/{post:slug}')) - Blade 组件语法 :简化组件使用(
<x-alert type="error"/>) - 并发测试支持 :通过
ParallelTesting加速测试套件 - 日志上下文增强:支持全局日志上下文数据
升级注意事项
- PHP 版本要求:≥ 7.2
- 兼容性调整 :如
helpers.php中array_*函数替换为Arr::* - 官方升级工具 :使用
laravel-shift自动化迁移
Laravel 6.x 通过模块化设计和性能优化,为大型应用提供了更稳定的开发基础。