where('task_id', $taskId) ->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks') ->whereColumn('tasks.id', 'task_comments.task_id') ->where('tasks.customer_job_id', $jobId)) ->orderBy('id') ->get(); $names = $this->users->namesByIds($models->pluck('user_id')->filter()->unique()->values()->all()); return $models ->map(fn (TaskCommentModel $model): TaskCommentSummary => new TaskCommentSummary( $model->id, $model->user_id, $model->user_id === null ? '' : ($names[$model->user_id] ?? ''), $model->body, $this->localTime($model->created_at), $model->edited_at === null ? null : $this->localTime($model->edited_at), )) ->values() ->all(); } private function localTime(CarbonImmutable $moment): string { return $moment->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'); } }