【问题标题】:Missing required parameters for [Route: user.profile][Route: user.profile] 缺少必需的参数
【发布时间】:2020-07-08 07:56:23
【问题描述】:

我正在尝试在我的 laravel 应用程序中创建用户个人资料,每个用户都可以查看每个用户的个人资料,但我遇到了错误

缺少 [Route: user.profile] 的必需参数

这是我的代码:

web.php

Route::get('user/profile/{id}', 'UserProfileController@profile')->name('user.profile');

控制器(UserProfileController)


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') );
    }

}

查看(profile.blade.php)


@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 offset-md-1">
            <div class="card">
                <div class="card-header">{{ $user->name }}'s Profile page</div>

                <div class="card-body">
                    Hi, {{ $user->name }} This is a private profile page!!!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

【问题讨论】:

  • 我认为问题出在app.blade.php,不在此处的代码中。它正在尝试创建路线,但没有通过$id。可以显示app.blade.php的相关部分吗?
  • 你的问题在app.blade.php,或者你可以从profile.blade.php删除@extends('app.blade.php'))
  • 没错!! &lt;a class="dropdown-item" href="{{ route('user.profile') }}"&gt; &lt;i class="fas fa-user"&gt;&lt;/i&gt; Profile &lt;/a&gt;我怎么传id?

标签: php laravel laravel-routing


【解决方案1】:

如果您仔细阅读错误,则该错误说明了 app.blade.php ,因此请转到 app.blade.php 并查找错误。 也许你应该寻找**

锚标签将id传递给它

**

为了进一步添加新版本的 laravel,您可以使用模型绑定来减少代码

公共功能配置文件(用户 $user)

{

           return view('user.profile')->withUser($user);
}

【讨论】:

  • 我这样做了,但我得到了错误 个人资料
  • $user->id 应该在那里
  • &lt;a class="dropdown-item" href="{{route('user.profile',$user-&gt;$id)}}"&gt; &lt;i class="fas fa-user"&gt;&lt;/i&gt; Profile &lt;/a&gt; 未定义变量:用户(查看:/home/riwaj/Desktop/dmt-intern-manager/InternManager/resources/views/layouts/app.blade.php)
  • 返回视图('user.profile')->with('user',$user);尝试一次或 dd($user) 在控制器中检查一次
  • 返回视频无效。你能澄清一下 dd($user)
【解决方案2】:

layouts/app.blade.php 中的问题 您忘记了所需的“id”参数。 使用{{route('user.profile', $id)}}

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多