public function add(Zipper $zipper, PathBuilder $pathBuilder, Request $request, BlueprintCollection $blueprintCollection, Filesystem $file, Repository $repository, Collection $collection) { $input = $request->get('collection'); // Check if the blueprint already belongs to this collection $search = $blueprintCollection->where(['collection_id' => $input['id'], 'blueprint_id' => $input['blueprint_id']])->get()->toArray(); if (!empty($search)) { return redirect()->back()->with('blueprintAlreadyInCollection', true); } // Insert the blueprint in the collection $insert = $blueprintCollection->insert(['collection_id' => $input['id'], 'blueprint_id' => $input['blueprint_id']]); if ($insert) { // Create an archive to server to the user rather than creating it when it's downloaded // Get all the collection current archives first $collection = $collection->select('id', 'archive_name', 'name')->where(['id' => $input['id']])->with(['blueprints' => function ($query) { $query->select('archive_name'); }])->first(); $buildcraftArchiveStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.archives')); // Delete the previous archive(if any) if (null !== $collection->archive_name) { $file->delete($buildcraftArchiveStoragePath . $collection->archive_name); } // Create a random name for the collection archive $collectionArchiveName = str_replace(' ', '', $collection->name) . '_' . getRandomId() . '_collection' . '.zip'; // Start creating the archive $zipper->make($buildcraftArchiveStoragePath . $collectionArchiveName); foreach ($collection->blueprints as $key => $value) { // Add each archive to the the newly created archive $zipper->add($buildcraftArchiveStoragePath . $value->archive_name); } //Clean up $zipper->close(); // Update the archive name in the collections table $collection->archive_name = $collectionArchiveName; $collection->save(); // Welp, that's that return redirect()->back()->with('blueprintAddedToCollection', true); } // Or not return redirect()->back()->with('blueprintNotAddedToCollection', true); }