为什么同步参数期望使用多个模型传递两个数组


Why sync parameter expect two array to be pass using multiple model?

有人能解释我为什么我需要在我的同步参数传递两个封闭数组[[]] ?我试图使用一个数组与多个模型内部,它不同步。我尝试了两个数组,它工作。任何提示将不胜感激!

$document = new Document();
foreach($request->recipient as $recipientId)
{
  $document->notifications()->sync([['user_id' => $recipientId, 'sender_id' => $user->id, 'notification_id' => 4]],false);
}

简单地说,sync方法期望附加模型的id数组,以及第二个数组中的任何其他枢轴更改。请查看下面文档中的示例:

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

您可以在文档https://laravel.com/docs/5.2/eloquent-relationships#inserting-many-to-many-relationships中找到有关同步方法期望的更多信息。

在最基本的层次上,sync方法接受一个id数组。您还可以同步数组的数组,其中每个子数组包含中间表值,这就是您正在做的。因为你只同步一个项目,所以要用双括号。