Implement file attachment functionality in ticket creation with upload preview and removal options

This commit is contained in:
2025-12-07 06:50:33 -05:00
parent eb44e03340
commit 9547a3589a
2 changed files with 89 additions and 4 deletions

View File

@@ -8,19 +8,37 @@ use App\Models\Ticket;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Storage;
use Livewire\WithFileUploads;
#[DisabledIf('tickets_disabled')]
class Create extends Component
{
public string $message;
use WithFileUploads;
public string $subject;
public string $message = '';
public string $department;
public string $subject = '';
public string $department = '';
public ?int $service = null;
public string $priority;
public string $priority = '';
public array $attachments = [];
public function updatedAttachments()
{
$this->validate([
'attachments.*' => 'image|max:5120', // 5MB max per image
]);
}
public function removeAttachment($index)
{
unset($this->attachments[$index]);
$this->attachments = array_values($this->attachments);
}
public function create()
{
@@ -31,6 +49,7 @@ class Create extends Component
'subject' => 'required|string',
'message' => 'required|string',
'priority' => 'required|in:low,medium,high',
'attachments.*' => 'image|max:5120',
]);
if (RateLimiter::tooManyAttempts('create-ticket', 1)) {
@@ -41,6 +60,19 @@ class Create extends Component
RateLimiter::increment('create-ticket', 30);
// 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),
];
}
}
$ticket = Ticket::create([
'user_id' => Auth::id(),
'department' => $this->department,
@@ -52,11 +84,13 @@ class Create extends Component
$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');
$this->redirect(route('tickets.show', $ticket), true);