Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automaticly make a profile when user registers (Laravel 5)

I'm trying to make a profile page for my registered users. On this page the Auth\User 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.

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).

In the 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 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('App\Profile');
    }
}

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}}

I'm guessing I need to add something to the 'AuthenticatesAndRegistersUsers' trait and the 'Registrar.php' service, but I have no idea what.

Thanks,

Cedric

like image 880
Pex Avatar asked Jun 02 '15 23:06

Pex


2 Answers

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.

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

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;
}
like image 61
Scopey Avatar answered Oct 19 '22 12:10

Scopey


There are three options that come to my mind.

Combine User and Profile tables

Why are you separating the user account from the profile? I can't think of a good reason to (not saying there isn't one, I just can't think of one). Combining the tables would save you database queries and completely resolve this issue. I think this would be the best option.

Use model event.

Create a listener on the User::created event.

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

Use a repository

Create a user repository to manage all the data base querying. Then in the repository create method you can manually create the profile record and associate the two. Then use the repository in the Registrar instead of the Model directly

like image 2
jfadich Avatar answered Oct 19 '22 11:10

jfadich