Laravel,sync() - 如何同步数组并传递额外的枢轴字段? [英] Laravel, sync() - how to sync an array and also pass additional pivot fields?

查看:34
本文介绍了Laravel,sync() - 如何同步数组并传递额外的枢轴字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方 Laravel 文档在 sync() 函数上有这个:

Official Laravel documentation has this on sync() function:

$user->roles()->sync( array( 1, 2, 3 ) );

您还可以将其他数据透视表值与给定的 ID 关联:

You may also associate other pivot table values with the given IDs:

$user->roles()->sync( array( 1 => array( 'expires' => true ) ) );

在后一个示例中,仅添加了一个枢轴行.我不明白的是,如果要同步的行不止一行,我如何关联其他数据透视表记录?

In the latter example only a single pivot row is being added. What I don't understand is how can I associate other pivot table records if there are more than one rows to be synced?

提前致谢.

推荐答案

现在有一个 ->syncWithPivotValues($ids, $pivotValues) 方法可用,如果你想设置相同的枢轴值适用于所有同步项目.

There is now a ->syncWithPivotValues($ids, $pivotValues) method available if you want to set the same pivot value for all synced items.

示例来自文档:

$user->roles()->syncWithPivotValues([1, 2, 3], ['active' => true]);

这篇关于Laravel,sync() - 如何同步数组并传递额外的枢轴字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆