Laravel-用户个人资料页面

时间:2019-09-22 22:52:30

标签: laravel

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

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

但是,我收到错误消息:未定义路由[profiles.show]。

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

我希望您能提供一些指导!这是我到目前为止的内容:


个人资料页面的链接

<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()->name }}

4 个答案:

答案 0 :(得分:0)

您的路线有误,请查看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'));
    }
}

答案 1 :(得分:0)

尝试

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

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

因此显示错误。在路由中,您需要传递ID。

p>

答案 2 :(得分:0)

问题已解决。在尝试将代码组合在一起并通读了数小时的“操作方法”手册之后,我发现了这个很棒的教程https://www.youtube.com/watch?v=yAVvOW64c0g,该教程非常简单地介绍了如何制作用户个人资料。有效。 ew!

答案 3 :(得分:0)

在刀片文件中:

<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>

您通过“个人资料”参数传递了用户信息。因此,您必须在刀片文件中写入此名称。

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

相关问题