【问题标题】:Laravel - User profile pagesLaravel - 用户个人资料页面
【发布时间】:2020-01-23 00:33:19
【问题描述】:

在我的应用程序中,我有一个用户表和一个配置文件表。当用户访问他们的仪表板时,他们应该能够单击一个链接来查看他们的个人资料页面。这是链接:

<a href="{{ route('profiles.show',$profiles->id)}}">link to your profile page</a>

但是,我收到错误消息:Route [profiles.show] not defined。

我是新手,不清楚如何将注册用户与他/她的个人资料页面相关联。所有用户在注册时都应该有一个个人资料页面。

不胜感激!这是我目前所拥有的:


个人资料页面的链接

<a href="{{ route('profiles.show',$profiles->id)}}">link to your profile page</a>

ProfilesController.php


namespace App\Http\Controllers;

use App\Profile;
use Illuminate\Http\Request;

class ProfilesController extends Controller
{
    public function show($id)
    {
        $profile = Profile::find($id);
        return view('profiles.show', compact('profile'));
    }
}

Profile.php


namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function user()
    {
        return $this->belongsTo('User');
    }
}

routes/web.php

Route::get('pages/profiles', 'ProfilesController@show');

profiles.blade.php

这只是一个非常简单的页面。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>{{ $user->user_id }}</h1>

    <p>{{ $user->about_me }}</p>
</body>

</html>

解决方案

我找到了一个简单的解决方案,我想在此处发布它以帮助其他可能在创建用户个人资料页面时遇到困难的人。下面假设您的数据库中已经有一个用户表,现在您想要创建一个配置文件表并将用户 ID 连接到他们的配置文件页面。 添加 Laravel 用户配置文件

我是the video,对我有帮助。

创建表

php artisan make:migration create_profiles_table 

这会创建一个迁移文件:

2019_09_22_213316_create_profiles_table

打开迁移文件并添加您需要的额外列:

$table->integer('user_id')->unsigned()->nullable();
$table->string('about_me')->nullable();

将这些迁移到数据库

php artisan migrate

现在我们已经对数据库进行了排序,我们需要创建一个控制器来控制我们的 php 功能。

ProfilesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
    public function show($user_id)
    {
        $user = User::find(1);
        $user_profile = Profile::info($user_id)->first();
        return view('profiles.show', compact('profile', 'user'));
    }

    public function profile()
    {
        return $this->hasOne('Profile');
    }
}

routes/web.php

Route::get('dashboard/profile', 'ProfilesController@show');

Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function user()
    {
        return $this->belongsTo('User');
    }
}

将此添加到 User.php

public function profile()
{
return $this->hasOne('Profile');
}

profile.blade.php

创建您想要的任何设计。如果你想拉入用户名,包括{{ Auth::user()-&gt;name }}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    试试这个

    Route::get('pages/profiles/{id}', 'ProfilesController@show')->name('profiles.show');
    

    因为在链接中您使用名称路由,但在 web.php 中没有名为profiles.show 的名称路由。

    所以它显示错误。并且在路线中您需要传递 ID。

    【讨论】:

    • 那没用,Undefined variable: profiles
    【解决方案2】:

    在刀片文件:

    <a href="{{ route('profiles.show',$profiles->id)}}">link to your profile page</a>
    

    如下更改路线样式:

    Route::get('pages/profiles/{id}', 'ProfilesController@show')->name('profiles.show');
    

    在个人资料模型中

    public function user()
    {
        return $this->belongsTo(User::class);
    }
    

    在 Profiles.blade.php 中

    <body>
      <h1>{{ $profile->id }}</h1>
    
       <p>{{ $profile->about_me }}</p>
    </body>
    

    您通过“profile”参数传递了用户信息。所以你必须把这个名字写在刀片文件中。

    注意如果您没有为该路线名称提及任何名称,则路线功能将不起作用。要么你必须使用 URL。

    【讨论】:

      【解决方案3】:

      你的路线错了,看看named routes。如果您不使用Route::resource(),则必须手动命名路由并指定何时需要参数(在本例中为配置文件 ID)。

      Route::get('pages/profiles/{id}', 'ProfilesController@show')->name('profiles.show');
      
      <a href="{{ route('profiles.show', $profile)}}">link to your profile page</a>
      

      Route model binding 在这种情况下可能是要走的路。

      Route::get('pages/profiles/{profile}', 'ProfilesController@show')->name('profiles.show');
      
      namespace App\Http\Controllers;
      
      use App\Profile;
      use Illuminate\Http\Request;
      
      class ProfilesController extends Controller
      {
          public function show(Profile $profile)
          {
              return view('profiles.show', compact('profile'));
          }
      }
      

      【讨论】:

      • 谢谢@azeós,我使用您的确切代码进行了更新,但仍然得到“未定义的变量:配置文件”
      • 在刀片文件中你使用

        {{ $profile->user_id }}

        {{ $profile->about_me }}

        body> 我改变我的答案
      • 有人能告诉我如何定义一个变量吗?我用谷歌搜索过它并在 Laravel 网站上,但没有什么是清楚的。无论我尝试过什么,我都会收到相同的错误“未定义的变量”。
      • @PHP75DJ 变量是$profile,单数。但我很困惑,如果您在 show() 方法中,为什么会有指向您已经存在的个人资料的链接?
      猜你喜欢
      • 2015-03-06
      • 2022-12-02
      • 2016-09-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2012-01-24
      • 2016-06-26
      • 2011-01-25
      相关资源
      最近更新 更多