input('type').'.zip'; $attachment = storage_path().'/app/documents/collections/' . $zip_file; $zip = new ZipArchive(); if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { $bookings = Booking::where('status', ApprovalStatus::COMPLETED) ->whereDate('updated_at', '>=', Carbon::parse($request->input('startDate'))) ->whereDate('updated_at', '<=', Carbon::parse($request->input('endDate')))->get(); if (!count($bookings)) throw new MalformedRequestException('No available file to download'); foreach ($bookings as $booking) { $file = $booking->documents()->where('document_type', $request->input('type'))->first()->files()->first(); $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); } if ($zip->close() === false) { exit("Error creating ZIP file"); }; readfile($attachment); unlink($attachment); } } }