我正在尝试为我的注册用户制作个人资料页面.在此页面上,将显示 AuthUser 数据(姓名、电子邮件)以及额外的个人资料信息(城市、国家/地区、电话号码等).
I'm trying to make a profile page for my registered users. On this page the AuthUser 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.
目前我只是通过 tinker 添加了我的第一个用户的个人资料,但是一旦我创建了第二个用户 &转到个人资料页面,它出错了(看到个人资料尚未制作).
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 IlluminateDatabaseEloquentModel;
class Profile extends Model {
protected $table = 'profiles';
protected $fillable = ['city', 'country', 'telephone'];
public function User()
{
return $this->belongsTo('AppUser');
}
}
在 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('AppProfile');
}
}
我像这样显示个人资料数据(在我的 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.
谢谢,
塞德里克
正如对您问题的评论中所述,我相信这里的最佳答案是将两个模型合并为一个 User 模型.
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 trait 将使用注册器(默认位于 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方法,同时自动创建profile关系:
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
MySQLi准备好的语句&foreach 循环MySQLi prepared statement amp; foreach loop(MySQLi准备好的语句amp;foreach 循环)
mysqli_insert_id() 是从整个服务器还是从同一用户获Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是从整个服务器还是从同一用户获取记录?)
PHP MySQLi 无法识别登录信息PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 无法识别登录信息)
mysqli_select_db() 需要 2 个参数mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 个参数)
Php mysql pdo 查询:用查询结果填充变量Php mysql pdo query: fill up variable with query result(Php mysql pdo 查询:用查询结果填充变量)
MySQLI 28000/1045 用户“root"@“localhost"的访问MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)