lundi 17 février 2020

How to update in one to one relation in laravel?

In my user model, I have the following code

public function profile()
{
    return $this->hasOne(UserProfile::class);
}

In profile model,

public function user()
{
   return $this->belongsTo(User::class);
}

The create method is working, But when I try to update the profile with following code, It is creating a new profile and doesn’t update the profile information.

$profile = new UserProfile();
$profile->dob = '1999-03-20';
$profile->bio = 'A professional programmer.';
$profile->facebook = 'http://facebook.com/test/1';
$profile->github = 'http://github.com/test/1';
$profile->twitter = 'http://twitter.com/test/1';

$user = User::find($userId);
$res = $user->profile()->save($profile);

What is the correct method to update in One to One Relationship ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire