用户注册时自动创建配置文件(Laravel 5) [英] Automaticly make a profile when user registers (Laravel 5)

查看:62
本文介绍了用户注册时自动创建配置文件(Laravel 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的注册用户创建个人资料页面. 在此页面上,将显示Auth \ User数据(名称,电子邮件),以及其他个人资料信息(城市,国家/地区,电话号码,..).

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, ..).

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

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

在Profile.php中,我有:

In the Profile.php I have:

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

}

在User.php中,我添加了:

In the User.php I added:

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

我这样显示个人资料数据(在我的profile.blade.php页面上):

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

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

我猜我需要在"AuthenticatesAndRegistersUsers"特征和"Registrar.php"服务中添加一些内容,但我不知道该怎么办.

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

谢谢

塞德里克

推荐答案

正如您对问题的评论所指出的那样,我认为最好的答案是将两个模型组合为一个用户模型.

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

但是,如果要在创建用户时在用户上创建关系,则可以修改注册服务.

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

AuthenticatesAndRegistersUsers特征将使用注册器(默认情况下位于app/Services/Registrar.php中)来验证和注册用户.

The AuthenticatesAndRegistersUsers trait will use the registrar (located in app/Services/Registrar.php by default) to validate and register users.

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

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

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

这篇关于用户注册时自动创建配置文件(Laravel 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆