isVisibleTo($request->user()), 403); $data = $request->validate([ 'photos' => ['required', 'array'], 'photos.*' => [ 'image', 'mimes:jpeg,png,jpg,webp', 'max:' . (config('system.asset.attachment_max_size_mb', 20) * 1024), ], ]); $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 { abort_unless(optional($asset_photo->asset)->isVisibleTo(request()->user()), 403); 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.'); } }