Laravel syncWithoutDetaching 和附加数据

reb*_*ion 6 laravel eloquent eloquent-relationship laravel-5.8

我用Google搜索我的手指疼,我不能见任何人讨论这个,但我有一个怀疑,LaravelssyncWithoutDetaching()方法没有考虑像额外的数据的任何参数save()sync()并且attach()呢?

有人知道这个吗?在 API 文档中,该方法具有以下参数:

array syncWithoutDetaching(Collection|Model|array $ids)

I have trouble adding existing data to a relationship between a Guest and an Event. I need to add status for the guests and what event they are attending, maybe attending or declined.

Rem*_*mul 8

sync()并且syncWithoutDetaching()两者都没有用于附加值的参数,您必须将附加值作为带有 id 的数组传递。


根据文档

您还可以使用 ID 传递其他中间表值:

$user->roles()->sync([
    1 => ['expires' => true],
    2,
    3
]);
Run Code Online (Sandbox Code Playgroud)


如果你看这里,你会看到它syncWithoutDetaching()只是调用,sync()false作为第二个参数传递。

在你的情况下,它会是这样的:

$event->guests()->syncWithoutDetaching([
    1 => ['attending' => true],
    2 => ['attending' => false]
])
Run Code Online (Sandbox Code Playgroud)