我有一个自定义功能来解析传入的电子邮件及其附件。当然,一封电子邮件可能包含多个附件。
我有这段代码:
public function parseEmail() {
// read from stdin
$fd = fopen("php://stdin", "r");
$rawEmail = "";
while (!feof($fd)) {
$rawEmail .= fread($fd, 1024);
}
fclose($fd);
$parser = new Parser();
$parser->setText($rawEmail);
$email = new Email;
$email->to = $parser->getHeader('to');
$email->from = $parser->getHeader('from');
$email->subject = $parser->getHeader('subject');
$email->body_text = $parser->getMessageBody('text');
$attachments = $parser->getAttachments();
$filesystem = new Filesystem;
foreach ($attachments as $attachment) {
$filesystem->put(public_path() . '/uploads/' . $attachment->getFilename(), $attachment->getContent());
$email->attachement()->name = $attachment->getFilename();
}
$email->save();
}
现在这段代码只能存储一个附件。但是如何更新以在模型中存储多个附件?
答案 0 :(得分:1)
您可以将此行$email->attachement()->name = $attachment->getFilename();
更改为$email->attachement()->name[] = $attachment->getFilename();
以推送到所有附件阵列,之后您可以将其序列化并保存,如果这对于商家逻辑可以。