Automatically create a profile when a user logs in (Laravel 5)

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

+5
source share
2 answers

As noted in the comments on your question, I believe that the best answer here is to combine the two models into one user model.

However, if you want to create a relationship for your user when you create it, you can change the registrar service.

The AuthenticatesAndRegistersUsers attribute will use the registrar (located by default app/Services/Registrar.php ) to verify and register users.

You can simply change the create method there to automatically create a 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; } 
+3
source

There are three options that come to me.

Join user and profile tables

Why do you separate a user account from a profile? I can’t come up with a good reason (not to say that there is nobody, I just can’t think about it). Joining tables will allow you to save database queries and completely solve this problem. I think this is the best option.

Use model event.

Create a listener in the User :: created event.

 User::created(function(User $user) { $user->profile->save(Profile::create([... ])); }); 

Use repository

Create a user repository to manage all database queries. Then, in the repository creation method, you can manually create a profile entry and link them. Then use the repository in the registrar instead of the model directly

+2
source

All Articles