*/ public function toArray(Request $request): array { return array_merge(parent::toArray($request), [ 'xp' => $this->totalXp(), ]); } private function totalXp(): int { if ($this->resource->relationLoaded('steps')) { return (int) $this->resource->steps->sum('xp'); } $stepsSumXp = $this->resource->getAttribute('steps_sum_xp'); if ($stepsSumXp !== null) { return (int) $stepsSumXp; } return (int) $this->resource->steps()->sum('xp'); } }