問題描述
我正在嘗試為我的注冊用戶制作個人資料頁面.在此頁面上,將顯示 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模板網!