假设我有'Post'型号和'Comment'型号 . A 'Post' can have multiple 'Comments' .

假设, I allow user to add/edit/delete multiple comments & 'submit' in a single form .

现在,当用户提交所有评论时,其中一些可能已被编辑,有些则没有,有些可能已删除,有些已添加 . 在这种情况下,我目前所做的是 - 'delete all the existing comments for that post & add all the submitted comments (no matter if they were edited/added) again' .

这导致我进入 problem - The autoincrement 'id' field of the 'comments' table started taking big values (even when the total number of comments is not equal to its value).

Solution I see is - Update the existing comments first & add more comments if they are actually added.

所以我的问题是 - 在这种情况下,laravel中有没有办法更新?对于前者 -

$post->comments()->update(array($comment1,$comment2));

哪里

$comment1 = array('comment_heading' => 'new/edited heading for comment',
'comment_content' => 'some new/edited content for comment');

其中'comment_heading','comment_content'是'comment'表的列 .

此外,本案例讨论了'oneToMany'的关系,但对'ManyToMany'关系的额外解决方案将不胜感激 .