Insert Collection to saveMany Eloquent

时间:2015-04-24 18:47:00

标签: php laravel eloquent laravel-5

我有一个填充模型的集合,我如何将这些放入关系中的$collection->toArray()方法?我知道它必须是一个数组,但当我public function uploadAndGetModels($messageId, array $attachments) { $models = new Collection(); $attachments = new Collection($attachments); $attachments->each(function (UploadedFile $attachment) use ($messageId, $models) { $identifier = $this->uploader->makeIdentifier($attachment->getClientOriginalExtension()); $this->uploader->uploadImage($attachment, "attachments/{$identifier}"); $model = new Attachment(array('message_id' => $messageId, 'path' => $identifier)); $models->push($model); }); return $models; } 时,它们就不再是雄辩的模型了。

$models

Collection的结果是Attachmentif ($request->hasFile('attachments')) { $attachments = $attachmentRepository->uploadAndGetModels($message->id, $request->file('attachments')); $message->attachments()->saveMany($attachments); } 模型。

Document.ready? do
  window = Native(`window`)
  window[:myvar] = MyClass.new
end

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:4)

Collection类有一个all()方法,它将集合中的所有项目作为模型数组返回。

考虑到这一点,这应该有用......

$message->attachments()->saveMany($attachments->all());