我正在try 更新拉维尔一对多的关系.不幸的是,我找不到它的任何文档.有人能帮我吗?
这就是我目前的情况:
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);
但这是行不通的:-(非常感谢您的帮助!!
更新:
以下工作:
$user = User::find(1);
$user->account_id = 99;
$user->save();
...但一定有一个更好的解决方案,像上面的那个,对吗?
它在多对多关系中使用save()和attach()方法来更新表之间的关系(从关系的两侧).在一对多关系中,似乎不支持attach()方法.