【发布时间】:2016-12-12 14:58:53
【问题描述】:
假设我们有这些表
Users table
-----------
id name
-----------
1 xxx
2 yyy
3 ccc
4 bbb
5 aaa
Location table
-------------
id name
-------------
6 Spain
7 Russia
8 Germany
9 USA
Pivot table
------------------------
id user_id location_id
------------------------
1 1 6
2 2 8
3 1 8
4 1 9
5 3 8
我想要实现的是同步数据透视表中的数据。 因此,我确实有用户 ID 数组 = [1,5,4] 和 location_id = 8 的发布请求。所以我会得到以下结果
[updated] Pivot table
-------------------------
id user_id location_id
-------------------------
1 1 6 <-- This one stays
3 1 8
4 1 9
6 5 8 <-- Added
7 4 8 <-- Added
...we deleted the row with location_id=8 and user_id=2 and user=3 because those are not in the users array
如何添加新的,删除不在请求中的,并保留已存在的具有某些功能的功能,就像 Laravel 在 sync 功能中所做的那样。
我知道最简单的方法是获取所有具有该特定 location_id 的用户,全部删除,然后再次插入。是否有一些解决方法或者我应该做新手的方式:D
谢谢
【问题讨论】: