Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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();
}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.