Does synchronization relapolation eliminate?

When updating the model and data synchronization, if I do not go through all the existing identifiers, will these relationships be deleted?

+4
source share
2 answers

You decided: it synchas a 2nd parameter, which is equal by default trueand is responsible for disconnecting:

$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]
+9
source

Answer: Yes. I could not find the documentation that actually claimed it.

let's say you have 2 tables: "authors" and "books", with a pivot table "book_authors".

when creating a new author:

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

You now have 4 entries in this book_authors pivot table:

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
0

Source: https://habr.com/ru/post/1544495/


All Articles