company->db); $this->settings = $this->company->settings; // create new zip object $zipFile = new \PhpZip\ZipFile(); $file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip'; $invitations = PurchaseOrderInvitation::query() ->with('purchase_order') ->whereIn('purchase_order_id', $this->purchase_order_ids) ->get(); $invitation = $invitations->first(); $path = $invitation->contact->vendor->purchase_order_filepath($invitation); try { foreach ($invitations as $invitation) { $file = (new CreateRawPdf($invitation))->handle(); $zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file); } Storage::put($path.$file_name, $zipFile->outputAsString()); $nmo = new NinjaMailerObject(); $nmo->mailable = new DownloadPurchaseOrders(Storage::url($path.$file_name), $this->company); $nmo->to_user = $this->user; $nmo->settings = $this->settings; $nmo->company = $this->company; NinjaMailerJob::dispatch($nmo); UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); } catch (\PhpZip\Exception\ZipException $e) { nlog('could not make zip => '.$e->getMessage()); } finally { $zipFile->close(); } } }