Attempt to fix N+1 issues still there and more now

This commit is contained in:
Mark Gregory
2026-06-16 20:15:22 +01:00
parent e81a509007
commit a07bfd13df
9 changed files with 65 additions and 72 deletions
+2 -2
View File
@@ -248,8 +248,8 @@ final class TicketEdit extends Component
private function freshTicket(Ticket $ticket): Ticket
{
return $ticket->fresh([
'owner',
'agent',
'owner.company',
'agent.company',
'status',
'priority',
'category',
+23 -7
View File
@@ -55,6 +55,7 @@ final class TicketShow extends Component
public function mount(Ticket $ticket): void
{
$this->actor()->loadMissing('company');
$ticket->loadMissing($this->ticketDetailRelations());
$this->authorize('view', $ticket);
@@ -85,7 +86,7 @@ final class TicketShow extends Component
'attachments.*' => 'file|max:'.config('helpdesk.max_upload_kb', 10240).'|mimes:'.implode(',', config('helpdesk.allowed_mimes', ['jpg', 'jpeg', 'png', 'pdf', 'txt', 'log', 'zip'])),
]);
$tickets->comment($this->ticket, Auth::user(), $this->commentBody, $this->internal, $this->attachments);
$tickets->comment($this->ticket, $this->actor(), $this->commentBody, $this->internal, $this->attachments);
$this->reset(['commentBody', 'internal', 'attachments']);
$this->ticket = $this->freshTicket($this->ticket);
@@ -102,7 +103,7 @@ final class TicketShow extends Component
'replyBody' => ['required', 'string', 'min:2'],
]);
$tickets->comment($this->ticket, Auth::user(), $this->replyBody, $this->internal, [], $this->reply_parent_id);
$tickets->comment($this->ticket, $this->actor(), $this->replyBody, $this->internal, [], $this->reply_parent_id);
$this->reset(['reply_parent_id', 'replyBody']);
$this->ticket = $this->freshTicket($this->ticket);
@@ -122,7 +123,7 @@ final class TicketShow extends Component
->where('is_active', true)
->findOrFail($this->canned_response_id);
$rendered = $renderer->render($response->body, $this->ticket, Auth::user());
$rendered = $renderer->render($response->body, $this->ticket, $this->actor());
$this->commentBody = trim($this->commentBody."\n\n".$rendered);
$this->dispatch('helpdesk-canned-response-applied', body: $this->commentBody);
}
@@ -135,7 +136,7 @@ final class TicketShow extends Component
->where('ticket_id', $this->ticket->id)
->findOrFail($itemId);
$checklists->toggle($item, Auth::user());
$checklists->toggle($item, $this->actor());
$this->ticket = $this->freshTicket($this->ticket);
}
@@ -176,7 +177,7 @@ final class TicketShow extends Component
$sla->recordClosure($this->ticket);
}
$workflow->dispatch($this->ticket, $status->is_closed ? 'ticket.closed' : 'ticket.updated', Auth::user());
$workflow->dispatch($this->ticket, $status->is_closed ? 'ticket.closed' : 'ticket.updated', $this->actor());
session()->flash('ticket-action-updated', 'Status updated to '.$this->ticket->status?->name.'.');
$this->dispatch('helpdesk-status-updated', statusId: $this->status_id);
@@ -212,7 +213,7 @@ final class TicketShow extends Component
$this->ticket = $this->freshTicket($this->ticket);
$this->priority_id = (int) $this->ticket->priority_id;
$workflow->dispatch($this->ticket, 'ticket.updated', Auth::user());
$workflow->dispatch($this->ticket, 'ticket.updated', $this->actor());
session()->flash('ticket-action-updated', 'Priority updated to '.$this->ticket->priority?->name.'.');
$this->dispatch('helpdesk-priority-updated', priorityId: $this->priority_id);
@@ -250,7 +251,7 @@ final class TicketShow extends Component
$this->ticket = $this->freshTicket($this->ticket);
$this->agent_id = $this->ticket->agent_id === null ? null : (int) $this->ticket->agent_id;
$workflow->dispatch($this->ticket, 'ticket.updated', Auth::user());
$workflow->dispatch($this->ticket, 'ticket.updated', $this->actor());
session()->flash('ticket-action-updated', $this->agent_id === null
? 'Ticket is now unassigned.'
@@ -261,6 +262,7 @@ final class TicketShow extends Component
public function render(): View
{
$this->actor()->loadMissing('company');
$this->ticket = $this->freshTicket($this->ticket);
return view('helpdesk::livewire.ticket-show', [
@@ -274,6 +276,19 @@ final class TicketShow extends Component
/**
* Convert Livewire select values into a nullable integer.
*/
/**
* Return the authenticated user with relations commonly used by policies and workflow templates.
*/
private function actor(): User
{
/** @var User $user */
$user = Auth::user();
$user->loadMissing('company');
return $user;
}
private function normaliseNullableInt(mixed $value): ?int
{
if ($value === null || $value === '' || $value === '0' || $value === 0) {
@@ -291,6 +306,7 @@ final class TicketShow extends Component
private function ticketDetailRelations(): array
{
return [
'user.company',
'owner.company',
'agent.company',
'status',