事情是这样的,我有 3 个表,users/users_types/types。
我通过 users_types 在用户和类型之间有一个 belongToMany,其中包含一些信息的枢轴。 其中之一是表格中的行号。 我试图根据 userID 和行号而不是 typeID 更新表,后者由我的输入填充。
我怎样才能让它发生? 我一直在努力
updateExistantPivot($line_number->line_number,array(
'type_id' => $type_id,
'etc' => $etc,
'duration' => $duration
)
);
但显然它不会工作,因为它需要 typeID 而不是 line_number...我总是想更新相同的 line_number 和 userID。 (我在每一行的 for 循环中)。
感谢您的帮助!
哦,我确实尝试过同步...但它给了我一个外键错误,因为它发送了不应该存在的数字。
最佳答案
如果你想更新现有的数据透视表,你可以这样做:
$model; // parent of the relation
$related; // related object already synced with the $model
$model->relation()->sync([$related->id => [ 'duration' => 'someValue'] ], false);
第一个参数是以相关模型 ID 作为键的数组,以及要更新的枢轴值数组,而第二个参数设置为 false
意味着您不会分离所有其他相关模型。
关于php - sync or updateExistingPivot with Laravel——如何根据第三个标准进行填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26059593/