在 laravel breeze 项目中,可通过 `auth::user()->id` 获取当前认证用户 id,并结合命名路由 `route(‘profile’, [‘id’ => …])` 正确生成带参数的个人资料页面链接,避免因参数格式错误导致路由解析失败。
要在 navigation.blade.php(如顶部下拉菜单)中为「Perfil」(个人资料)添加指向当前用户专属档案页的链接,关键在于正确传递命名路由所需的参数。你的路由定义如下:
Route::group([
'prefix' => 'profile',
'as' => 'profile.',
'middleware' => ['auth']
], function () {
Route::get('/{id}', [ProfileController::class, 'show'])->name('profile');
});
该路由声明了必需的 {id} 参数,且使用了命名 ‘profile’(注意:as => ‘profile.’ 表示前缀,实际完整名称为 profile.profile?但根据你调用 route(‘profile’) 推断,更可能是 as => ‘profile’ —— 请确认 php artisan route:list 输出中该路由的 Name 列是否确为 profile;若为 profile.show 或其他,请以实际名称为准)。
✅ 正确写法(在 Blade 模板中):
Auth::user()->id])">
{{ __('Perfil') }}
⚠️ 常见错误分析:
- :href=”{{ route(‘profile’, [Auth::user()->id]) }}” ❌
→ 传入的是数值数组 [123],Laravel 路由生成器期望的是关联数组(键名需与路由参数名一致),否则会抛出 Missing required parameters for route 错误。 - :href=”{{ route(‘profile’,/([Auth::user()->id])) }}” ❌
→ 语法非法,PHP 解析失败,根本无法编译。
? 进阶建议(提升健壮性):