我有下一个关系User hasMany Contacts
.在正常情况下,使用$user->contacts()->save($contact)
将联系人添加到用户并将其保存到用户,但是我需要将联系人与用户关联而不保存模型(用户和联系人)。
编辑:
我需要构建一个接收此类方法之一的工厂,并向 XML 返回模板集合,只有在其中一些模型中才会存储在 postiriori。
您可以将模型上的associate
与 belongsTo
一起使用。
$contact= Contact::find(10);
$user->account()->associate($contact);
$user->save(); // You do need to update your user
来源: http://laravel.com/docs/eloquent
如果您知道UserID
,只需创建一个new Contact;
并确保在user_id
列中具有正确的用户 ID。 甚至不必触摸User
.