Skip to main content
The Attachment model handles file uploads and storage for messages and group covers. It supports both local and cloud storage (like S3) with automatic URL generation.

Namespace

Wirechat\Wirechat\Models\Attachment

Properties

id
int
Unique identifier for the attachment
attachable_type
string
The model type this attachment belongs to (polymorphic)
attachable_id
int
The ID of the model this attachment belongs to
file_path
string
Storage path to the file
file_name
string
Generated unique file name
original_name
string
Original file name from upload
url
string
Generated URL to access the file (computed attribute)
mime_type
string
MIME type of the file (e.g., “image/png”, “application/pdf”)
created_at
Carbon|null
Upload timestamp
updated_at
Carbon|null
Last update timestamp

Relationships

attachable()

Get the model that owns this attachment.
public function attachable(): MorphTo
return
MorphTo
Polymorphic relationship to parent model (Message or Group)
Example:
$attachment = Attachment::find(1);
$owner = $attachment->attachable;

if ($owner instanceof Message) {
    echo "Attached to message: {$owner->body}";
} elseif ($owner instanceof Group) {
    echo "Group cover for: {$owner->name}";
}

Accessors

url

Get the full URL to access the file.
protected function url(): Attribute
return
string|null
The full URL to the file, or null if no file path exists
Behavior:
  • For public disks: Returns a standard public URL
  • For private disks: Returns a temporary signed URL (5 minutes expiration)
  • Automatically uses the configured WireChat storage disk
Example:
$attachment = Attachment::find(1);
echo $attachment->url; // Automatically generates appropriate URL

clean_mime_type

Get the simplified MIME type (removes the prefix).
public function getCleanMimeTypeAttribute(): string
return
string
The file type without the category prefix (e.g., “png” instead of “image/png”)
Example:
// If mime_type is "image/png"
echo $attachment->clean_mime_type; // Outputs: "png"

// If mime_type is "application/pdf"
echo $attachment->clean_mime_type; // Outputs: "pdf"

Storage Management

Attachments automatically handle file deletion when the model is deleted. Automatic File Deletion:
// Deleting an attachment also deletes the physical file
$attachment->delete();
// File at $attachment->file_path is automatically removed from storage

Example Usage

use Wirechat\Wirechat\Models\Attachment;
use Illuminate\Support\Facades\Storage;

// Upload file for a message
$file = $request->file('attachment');
$path = $file->store('chat-attachments', Wirechat::storage()->disk());

$attachment = Attachment::create([
    'attachable_type' => Message::class,
    'attachable_id' => $message->id,
    'file_path' => $path,
    'file_name' => $file->hashName(),
    'original_name' => $file->getClientOriginalName(),
    'mime_type' => $file->getMimeType(),
]);

echo "File uploaded: {$attachment->url}";

URL Generation

The url attribute automatically generates the correct URL based on storage configuration:

File Deletion

When an attachment is deleted, the physical file is automatically removed:
// Delete attachment (also deletes file from storage)
$attachment->delete();

// The file at $attachment->file_path is removed from the configured disk

Supported MIME Types

Common MIME types handled by WireChat:
TypeMIME TypeExtension
Imageimage/png.png
Imageimage/jpeg.jpg, .jpeg
Imageimage/gif.gif
Imageimage/webp.webp
Videovideo/mp4.mp4
Videovideo/webm.webm
Audioaudio/mpeg.mp3
Audioaudio/wav.wav
Documentapplication/pdf.pdf
Documentapplication/msword.doc
Documentapplication/vnd.openxmlformats-officedocument.wordprocessingml.document.docx

Notes

  • Attachments are polymorphic and can belong to Messages or Groups
  • The url attribute is computed dynamically based on storage configuration
  • Private storage automatically generates temporary signed URLs
  • Deleting an attachment also removes the physical file
  • File names are hashed to prevent collisions and ensure uniqueness
  • Original file names are preserved for display purposes
  • The attachment model respects WireChat’s global storage configuration