I am trying to update the relationship of a one-to-many relationship in Laravel. Unfortunately I couldn't find any documentation for it. Can anyone help me?
This is what I have so far:
class Account extends Eloquent
{
public function users()
{
return $this->hasMany('User');
}
}
class User extends Eloquent
{
public function account()
{
return $this->belongsTo('Account');
}
}
Now I am trying to update the relationship from USER(1) > ACCOUNT(50) to USER(1) > ACCOUNT(99). How would I do this? I tried the following:
$account = Account::find(99);
User::find(1)->account()->save($account);
But that doesn't work :-( Any help deeply appreciated!!
UPDATE:
The following works:
$user = User::find(1);
$user->account_id = 99;
$user->save();
...but there MUST be a better solution like the one above, right?
It works in many-to-many relationships with both the save() and attach() method to update the relationship between tables (from both sides of the relationship). In one-to-many relationships the attach() method doesn't seem to be supported.