You can implement the "boot" method of User model, thus the profile is automatically created when a new user is saved.
User.php
class User extends Eloquent implements UserInterface, RemindableInterface {
{
// ...
/**
* The "booting" method of the model.
*
* @return void
*/
public static function boot()
{
parent::boot();
static::created(function($model)
{
$profile = new Profile;
$profile->user_id = $model->id;
$profile->save();
});
}
// ....
}
How to use
UserController.php
public function postCreate() {
// ...
$user->save();
$user->profile->bio = "new bio";
$user->profile->save();
}
You don't really need the check $user->Profile ?: new Profile;
as they are one-one relations.
If user doesn't exists, even profile won't.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community