39 lines
858 B
PHP
39 lines
858 B
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class HuntResource extends JsonResource
|
|
{
|
|
public static $wrap = null;
|
|
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
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');
|
|
}
|
|
}
|