Laravel / Eloquent BelongsToMany同步多个关系

时间:2015-08-11 16:36:45

标签: php laravel eloquent

我正在尝试一次更新多个关系,但是,只有最终的同步调用实际上会在以下代码中保存到数据库中:

// find article by ID
$article = Article::find($id);

// Update status relationships
$article->authoringStatus()->sync([$request->authoringStatus]);
$article->publicationStatus()->sync([$request->publicationStatus]); // Only this one syncs

// save the rest of the request data
$article->update($request->all());

如果我交换同步语句,仍然只有最后一个保存到数据库。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:2)

是的,这是因为同步会从数据透视表中删除所有记录,然后插入新记录。第二个同步是覆盖第一次同步中插入的任何内容。

相关问题