validated(); try { $files = collect($validated['documents']) ->map(fn ($document): File => $fileService->saveUploadedFile( "production-orders/{$productionOrder->id}/documents", $document, )); $productionOrder->documents()->syncWithoutDetaching($files->pluck('id')); } catch (Throwable $exception) { report($exception); return back()->with('error', 'Не удалось загрузить документы.'); } return back()->with('success', 'Документы загружены.'); } public function destroyDocument( ProductionOrder $productionOrder, File $file, FileService $fileService, ): RedirectResponse { abort_unless($productionOrder->documents()->whereKey($file->id)->exists(), 404); $productionOrder->documents()->detach($file->id); $fileService->deleteFileWithThumbnail($file); $file->delete(); return back()->with('success', 'Документ удалён.'); } public function storePhotos( UploadProductionOrderPhotosRequest $request, ProductionOrder $productionOrder, FileService $fileService, ): RedirectResponse { $validated = $request->validated(); try { $files = collect($validated['photos']) ->map(fn ($photo): File => $fileService->saveUploadedFile( "production-orders/{$productionOrder->id}/photos", $photo, )); $productionOrder->photos()->syncWithoutDetaching($files->pluck('id')); } catch (Throwable $exception) { report($exception); return back()->with('error', 'Не удалось загрузить фотографии.'); } return back()->with('success', 'Фотографии загружены.'); } public function destroyPhoto( ProductionOrder $productionOrder, File $file, FileService $fileService, ): RedirectResponse { abort_unless($productionOrder->photos()->whereKey($file->id)->exists(), 404); $productionOrder->photos()->detach($file->id); $fileService->deleteFileWithThumbnail($file); $file->delete(); return back()->with('success', 'Фотография удалена.'); } public function storePassport( Request $request, ProductionOrder $productionOrder, ProductionOrderItem $item, FileService $fileService, ): RedirectResponse { $this->ensureItemBelongsToOrder($productionOrder, $item); $validated = $request->validate([ 'passport' => ['required', 'file', 'max:10240'], ]); $previousPassport = $item->passportFile; try { $file = $fileService->saveUploadedFile( "production-orders/{$productionOrder->id}/items/{$item->id}/passport", $validated['passport'], ); $item->update(['passport_file_id' => $file->id]); if ($previousPassport) { $fileService->deleteFileWithThumbnail($previousPassport); $previousPassport->delete(); } } catch (Throwable $exception) { report($exception); return back()->with('error', 'Не удалось загрузить паспорт.'); } return back()->with('success', 'Паспорт загружен.'); } public function destroyPassport( ProductionOrder $productionOrder, ProductionOrderItem $item, FileService $fileService, ): RedirectResponse { $this->ensureItemBelongsToOrder($productionOrder, $item); $file = $item->passportFile; abort_unless($file, 404); $item->update(['passport_file_id' => null]); $fileService->deleteFileWithThumbnail($file); $file->delete(); return back()->with('success', 'Паспорт удалён.'); } private function ensureItemBelongsToOrder(ProductionOrder $productionOrder, ProductionOrderItem $item): void { abort_unless((int) $item->production_order_id === (int) $productionOrder->id, 404); } }