雄辩的关系同步也删除



当更新模型并同步关系时,如果我没有传递已经存在的所有id,该关系会被删除吗?

您决定:sync有第二个参数,默认为true,并负责分离:

$model->relationship()->sync([1,2,3]);
$model->relationship()->sync([4,5,6]); // attached [4,5,6], detached [1,2,3]
$model->relationship()->getRelatedIds(); // [4,5,6]
// but:
$model->relationship()->sync([4,5,6], false); // attached [4,5,6], detached []
$model->relationship()->getRelatedIds(); // [1,2,3,4,5,6]

答案是肯定的。我找不到任何实际说明这一点的文件

假设你有两个表:"authors"one_answers"books",还有一个透视表"book_authors"。

创建新作者时:

$author_id =2;
$author->books()->sync(array(1,4,5,15));

现在数据透视表"book_authors"中有4个条目:

author_id  book_id
2          1
2          4
2          5
2          15
现在更新:

$author_id =2;
$author->books()->sync(array(1,15));

现在"book_authors"为:

author_id  book_id
    2          1
    2          15

最新更新