make(Illuminate\Contracts\Console\Kernel::class); $kernel->bootstrap(); // ID from your error message $id = '019b8920-93ee-7052-9282-b6f6b320b741'; echo "--- Attachment Debugger ---\n"; echo "Target ID: $id\n\n"; // 1. Check Database echo "[1] Checking Database Record...\n"; $attachment = TicketAttachment::find($id); if (!$attachment) { echo "ERROR: Record not found in database.\n"; exit(1); } echo "Found Record!\n"; echo "- ID: " . $attachment->id . "\n"; echo "- File Name: " . $attachment->file_name . "\n"; echo "- Stored Path: " . $attachment->file_path . "\n"; echo "- Created At: " . $attachment->created_at . "\n"; // 2. Check Disk $path = $attachment->file_path; $disk = 'r2-private'; echo "\n[2] Checking R2 Private Storage...\n"; echo "Disk: $disk\n"; echo "Path: $path\n"; try { if (filter_var($path, FILTER_VALIDATE_URL)) { echo "WARNING: Path is a full URL ($path). This logic is legacy/public.\n"; exit; } $exists = Storage::disk($disk)->exists($path); if ($exists) { echo "STATUS: FILE EXISTS ✅\n"; // Try reading first 100 bytes $content = Storage::disk($disk)->get($path); echo "Read Test: Success (" . strlen($content) . " bytes read)\n"; } else { echo "STATUS: FILE NOT FOUND ❌\n"; echo "Diagnosis: Upload failed or path mismatch.\n"; } // 3. Check Relationships (Simulate Controller Auth) echo "\n[3] Checking Relationships (Auth Logic)...\n"; $reply = $attachment->reply; if (!$reply) { echo "FATAL: Attachment has NO linked Reply (ticket_reply_id: " . ($attachment->ticket_reply_id ?? 'NULL') . ").\n"; echo "Controller Logic `$attachment->reply->ticket` will CRASH here.\n"; } else { echo "Reply Found: ID " . $reply->id . "\n"; $ticket = $reply->ticket; if (!$ticket) { echo "FATAL: Reply has NO linked Ticket.\n"; echo "Controller Logic `$ticket->user_id` will CRASH here.\n"; } else { echo "Ticket Found: ID " . $ticket->id . " (Owner: " . $ticket->user_id . ")\n"; echo "Auth Logic seems SAFE.\n"; } } } catch (\Exception $e) { echo "EXCEPTION: " . $e->getMessage() . "\n"; }