php - Automaticly make a profile when user registers (Laravel 5) -
i'm trying make profile page registered users. on page auth\user data displayed (name, email) profile information (city, country, phone number, ..).
i've made 1 one relationship i'm having 1 issue. when user gets created, automaticly have profile created specific user.
at moment added profile first user through tinker, made second user & went profile page, gave error (seeing profile had not been made yet).
in profile.php 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 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 (on 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 need add 'authenticatesandregistersusers' trait , 'registrar.php' service, have no idea what.
thanks,
cedric
as noted in comments on question believe best answer here combine 2 models 1 user model.
however, if want create relationship on user when created can modify registrar service.
the authenticatesandregistersusers
trait use registrar (located in app/services/registrar.php
default) validate , register users.
you can modify create
method in there automatically create profile relation @ 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; }
Comments
Post a Comment