首页 > 解决方案 > 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)

SS 错误

标签: phplaravellaravel-routing

解决方案


您需要隐式声明 id :

<a class="dropdown-item" href="{{route('user.profile', ['id' => $user->id])}}">Profile </a>

推荐阅读