mercredi 31 mai 2017

How to add add multiple values to modell before save in Laravel?

I have a custom fuction to parse incoming emails and their attachements. Of course one email may contain multiple attachement.

I have this code:

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();

}

Now this code can store only one attachement. But how can I update to store multiple attachements in model?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire