I am trying to create a profile page for my registered users. This page will display the Auth \ User data (Name, Email), as well as additional profile information (city, country, phone number, ..).
I already made a one-to-one relationship, but I have one problem. When a user is created, I would like to automatically create a profile created for that particular user.
At the moment, I just added a profile for my first user via tinker, but as soon as I made the second user and went to the profile page, he gave an error (there was no profile view yet).
In 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'); } }
In 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'); } }
I show 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}}
I suppose I need to add something to the AuthenticatesAndRegistersUsers attribute and to the Registrar.php service, but I have no idea what.
Thanks,
Cedric