Add file attachment functionality to ticket messages

This commit is contained in:
2025-12-07 06:44:36 -05:00
parent 355f435fa1
commit 16c61494c8
5 changed files with 163 additions and 1 deletions

View File

@@ -9,28 +9,79 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\WithFileUploads;
#[DisabledIf('tickets_disabled')]
class Show extends Component
{
use WithFileUploads;
#[Locked]
public Ticket $ticket;
#[Rule('required', 'string')]
public string $message;
public string $message = '';
#[Rule(['attachments.*' => 'image|max:5120'])] // 5MB max per image
public array $attachments = [];
public function updatedAttachments()
{
$this->validateOnly('attachments.*');
}
public function removeAttachment($index)
{
unset($this->attachments[$index]);
$this->attachments = array_values($this->attachments);
}
public function uploadImage()
{
$this->validate([
'attachments.*' => 'image|max:5120',
]);
$uploadedPaths = [];
foreach ($this->attachments as $attachment) {
$path = $attachment->store('ticket-attachments', 'public');
$uploadedPaths[] = [
'path' => $path,
'name' => $attachment->getClientOriginalName(),
'url' => Storage::url($path),
];
}
return $uploadedPaths;
}
public function save()
{
$this->validate();
// Handle attachments
$attachmentData = [];
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$path = $attachment->store('ticket-attachments', 'public');
$attachmentData[] = [
'path' => $path,
'name' => $attachment->getClientOriginalName(),
'url' => Storage::url($path),
];
}
}
$this->ticket->messages()->create([
'user_id' => Auth::id(),
'message' => $this->message,
'attachments' => !empty($attachmentData) ? $attachmentData : null,
]);
$this->notify('Message sent successfully');
$this->message = '';
$this->attachments = [];
$this->dispatch('saved');
}