php - Laravel:在 href 中传递 {id}
问题描述
我正在尝试在 href 链接中传递用户 ID,如下所示:-
app.blade.php
<a class="dropdown-item" href="{{route('user.profile', $user->id)}}">Profile </a>
应用程序.php
Route::get('user/profile/{id}', 'UserProfileController@profile')->name('user.profile');
控制器 UserProfileController.js
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserProfileController extends Controller
{
//
public function __construct()
{
$this->middleware('auth', ['except' => [ 'profile']]);
}
public function profile($id)
{
$user = User::find($id);
return view('user.profile', compact('user') );
}
}
错误
未定义变量:用户(查看:/home/riwaj/Desktop/dmt-intern-manager/InternManager/resources/views/layouts/app.blade.php)
解决方案
您需要隐式声明 id :
<a class="dropdown-item" href="{{route('user.profile', ['id' => $user->id])}}">Profile </a>
推荐阅读
- sql - 从 VS Data 工具中动态选择表名
- javascript - 如何将 Mozilla ActiveX Control v1.7.12 添加到我的 vb.net windows 窗体项目中
- javascript - 使用 es6 创建嵌套对象
- c++ - QProcess 不运行外部应用程序
- dataset - 如何下载 knime 分区测试和训练文件
- wordpress - 带有应用程序负载平衡器的 Wordpress 导致 301 重定向
- python - Django Cron,从视图执行 def
- mongodb - 找不到我要打印的集合
- java - 尽管 PATH 和 JAVA_HOME 设置正确,但 java 不是公认的命令错误
- python - 尝试使用 Discord.py rewrite 向特定频道发送消息,但它不起作用