我正在尝试为我的注册用户创建一个个人资料页面。
在此页面上,将显示身份验证\用户数据(姓名、电子邮件),还会显示额外的个人资料信息(城市、国家/地区、电话号码等)。
我已经建立了一对一的关系,但我遇到了一个问题。
创建用户后,我想自动为该特定用户创建一个配置文件。
目前,我只是通过 Tinker 添加了第一个用户的个人资料,但是当我创建第二个用户并进入个人资料页面时,它给出了一个错误(看到个人资料尚未创建)。
在 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');
}
}
在 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');
}
}
我像这样显示配置文件数据(在我的 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}}
我猜我需要向“AuthenticatesAndRegistersUsers”特征和“Registrar.php”服务添加一些内容,但我不知道是什么。
Thanks,
Cedric