【问题标题】:Passing a user id in a url in laravel在 laravel 的 url 中传递用户 ID
【发布时间】:2019-01-11 16:19:56
【问题描述】:

我正在制作一个 Laravel 网络应用程序,在这个应用程序中,我有一个帐户设置页面。在这个页面上,我想显示用户下的所有订单。为此,我必须从 URL 中获取用户 ID。

我尝试通过将 Auth::user()->id 放入指向页面的路由中来通过 URL 传递用户 ID。但是,这给了我一个 404 not found 错误。

这是应该在 URL 中传递 ID 的链接。

 <a class="dropdown-item" href="{{ route('account/{{ Auth::user()->id }}') }}">
 {{ __('Account Settings') }}
 </a>

我也尝试更改我的 web.php 文件,但这也没有给出任何结果。

我真的很感激能帮我解决我的问题,因为我整天都被困在这个问题上。

【问题讨论】:

标签: php laravel frameworks


【解决方案1】:

您正在使用 Laravel 路由助手。该路线仅适用于 NAMED ROUTE, 例如:

Route::get('account/{userId}', 'AccountController@show')->name('account');

那么,你的可能应该是:

<a class="dropdown-item" href="{{ route('account', ['userId' => Auth::id()]) }}">
 {{ __('Account Settings') }}
</a>

【讨论】:

  • 或者在href上,如果你只有一个参数,你可以这样做:{{route('account ', Auth::id())}}
猜你喜欢
  • 1970-01-01
  • 2014-04-26
  • 2020-07-08
  • 2017-05-16
  • 1970-01-01
  • 2016-01-25
  • 2017-05-21
  • 1970-01-01
  • 2020-06-01
相关资源
最近更新 更多