Add file attachment functionality to ticket messages
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user