用户注册时自动创建个人资料(Laravel 5)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30608069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:42:19  来源:igfitidea点击:

Automaticly make a profile when user registers (Laravel 5)

phplaraveleloquentlaravel-5

提问by Pex

I'm trying to make a profile page for my registered users. On this page the Auth\User data will be displayed (Name, Email) but also extra profile information (city, country, phone number, ..).

我正在尝试为我的注册用户制作个人资料页面。在此页面上,将显示 Auth\User 数据(姓名、电子邮件)以及额外的个人资料信息(城市、国家/地区、电话号码等)。

I've already made the one to one relationship but I'm having one issue. When a User gets created, I would like to automaticly have a Profile created for that specific user.

我已经建立了一对一的关系,但我有一个问题。创建用户后,我想自动为该特定用户创建一个配置文件。

At the moment I simply added the profile for my first user through tinker, but as soon as I made a second user & went to the profile page, it gave an error (seeing the profile had not been made yet).

目前,我只是通过 tinker 为我的第一个用户添加了个人资料,但是一旦我创建了第二个用户并转到了个人资料页面,它就会出现错误(看到尚未创建个人资料)。

In the Profile.php I have:

在 Profile.php 我有:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model {

    protected $table = 'profiles';
    protected $fillable = ['city', 'country', 'telephone'];

    public function User()
    {
        return $this->belongsTo('App\User');
    }

}

In the User.php I added:

在 User.php 我添加:

<?php namespace App;

...

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    ...

    protected $table = 'users';

    protected $fillable = ['name', 'lastname', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];


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

I show the Profile data like this (on my profile.blade.php page):

我像这样显示配置文件数据(在我的 profile.blade.php 页面上):

Full name: {{ Auth::user()->name }} {{ Auth::user()->lastname }}
E-Mail Address: {{ Auth::user()->email}}


City: {{ Auth::User()->profile->city}}
Country: {{ Auth::User()->profile->country}}
Phone number: {{ Auth::User()->profile->telephone}}

I'm guessing I need to add something to the 'AuthenticatesAndRegistersUsers' trait and the 'Registrar.php' service, but I have no idea what.

我猜我需要向“AuthenticatesAndRegistersUsers”特征和“Registrar.php”服务添加一些内容,但我不知道是什么。

Thanks,

谢谢,

Cedric

塞德里克

采纳答案by Scopey

As noted in the comments on your question I believe the best answer here is to combine the two models into one User model.

正如对您的问题的评论中所述,我相信这里的最佳答案是将两个模型合并为一个 User 模型。

However, if you want to create a relationship on your user when it is created you can modify the Registrar service.

但是,如果您想在创建用户时为其创建关系,则可以修改 Registrar 服务。

The AuthenticatesAndRegistersUserstrait will use the registrar (located in app/Services/Registrar.phpby default) to validate and register users.

AuthenticatesAndRegistersUsers特性将使用登记处(位于app/Services/Registrar.php默认情况下)来验证和注册用户。

You can just modify the createmethod in there to automatically create the profile relation at the same time:

您可以修改其中的create方法以同时自动创建配置文件关系:

public function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
    $user->profile()->save(new Profile);
    return $user;
}

回答by jfadich

There are three options that come to my mind.

我想到了三个选项。

Combine User and Profile tables

合并用户和配置文件表

Why are you separating the user account from the profile? I can't think of a good reason to (not saying there isn't one, I just can't think of one). Combining the tables would save you database queries and completely resolve this issue. I think this would be the best option.

为什么要将用户帐户与个人资料分开?我想不出一个很好的理由(不是说没有,我只是想不出一个)。组合这些表将节省您的数据库查询并完全解决此问题。我认为这将是最好的选择。

Use model event.

使用模型事件。

Create a listener on the User::created event.

在 User::created 事件上创建一个侦听器。

User::created(function(User $user) {
    $user->profile->save(Profile::create([... ]));
});

Use a repository

使用存储库

Create a user repository to manage all the data base querying. Then in the repository create method you can manually create the profile record and associate the two. Then use the repository in the Registrar instead of the Model directly

创建一个用户存储库来管理所有数据库查询。然后在repository create方法中你可以手动创建profile记录并将两者关联起来。然后直接使用Registrar中的repository而不是Model