Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

_Marco_'s avatar

Sync() in One to Many relation

Is there a way to use sync() or a similar function in a one to many relation??

0 likes
3 replies
MichalOravec's avatar

There is nothing like that.

You can just do somethin like this

$post->comments()->delete();

$post->comments()->saveMany([
    new App\Comment(['message' => 'A new comment.']),
    new App\Comment(['message' => 'Another comment.']),
]);
_Marco_'s avatar
_Marco_
OP
Best Answer
Level 1

I can't delete them all because there is a different relation with this entity in a poly pivot and the id will change if I do this.

So to explain better: Entity A haves one to many relation with Entity B. Entity B haves a poly relation with Entity C and Entity D.

If I delete them all and create them again id will change and poly will not work anymore.

Please or to participate in this conversation.