Automaticly make a profile when user registers (Laravel 5)(用户注册时自动创建个人资料(Laravel 5))
问题描述
我正在尝试为我的注册用户制作个人资料页面.在此页面上,将显示 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用户注册时自动创建个人资料(Laravel 5)
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01