with('subcategories')->get(); } #[Computed] public function selectedCategory(): ?Category { return $this->categoryId ? Category::query()->with('subcategories')->find($this->categoryId) : null; } #[Computed] public function selectedSubcategory(): ?Subcategory { return $this->subcategoryId ? Subcategory::query()->with('customFields')->find($this->subcategoryId) : null; } public function selectCategory(int $id): void { $this->categoryId = $id; $this->subcategoryId = null; $this->step = 2; } public function selectSubcategory(int $id): void { $this->subcategoryId = $id; $this->step = 3; } /** * @return array */ #[Computed] public function suggestedArticles(): array { $query = trim(($this->selectedCategory?->name ?? '').' '.($this->selectedSubcategory?->name ?? '')); return app(BookStackClient::class)->search($query); } public function backToCategory(): void { $this->step = 1; } public function backToSubcategory(): void { $this->step = 2; } public function updatedAttachments(): void { if (! $this->attachments) { return; } if ($error = Settings::validateAttachments($this->attachments)) { $this->attachments = []; $this->addError('attachments', $error); } } public function removeAttachment(int $index): void { unset($this->attachments[$index]); $this->attachments = array_values($this->attachments); } public function submit(): void { $rules = [ 'subject' => 'required|string|max:255', 'body' => 'required|string', ]; foreach ($this->selectedSubcategory?->customFields ?? [] as $field) { $rules["customValues.{$field->id}"] = $field->required ? 'required' : 'nullable'; } $this->validate($rules); $user = Auth::user(); $ticket = app(TicketService::class)->create([ 'email' => $user->email, 'subcategory_id' => $this->subcategoryId, 'subject' => $this->subject, 'body' => $this->body, 'custom_values' => $this->customValues, ], $user); app(TicketService::class)->attachFiles($ticket, $ticket->messages()->first(), $this->attachments); $this->redirect(route('client.ticket', $ticket), navigate: true); } public function render() { return view('livewire.client.new-ticket'); } }