validate([ 'photos' => ['required', 'array'], 'photos.*' => ['image', 'mimes:jpeg,png,jpg,webp', 'max:4096'], ]); $stored = 0; foreach ($data['photos'] as $file) { $path = $file->store("assets/{$asset->id}", 'public'); AssetPhoto::create([ 'asset_id' => $asset->id, 'path' => $path, 'is_primary' => $asset->photos()->count() === 0, ]); $stored++; } return back()->with('success', "{$stored} foto aset berhasil diunggah."); } public function destroy(AssetPhoto $asset_photo): RedirectResponse { Storage::disk('public')->delete($asset_photo->path); $asset = $asset_photo->asset; $wasPrimary = $asset_photo->is_primary; $asset_photo->delete(); if ($wasPrimary) { $next = $asset->photos()->first(); if ($next) { $next->update(['is_primary' => true]); } } return back()->with('success', 'Foto aset berhasil dihapus.'); } }