getRenderableFields(), $this->getDirectRelations(), $this->getAllowedPivots(), ); } /** * Get the renderable fields for the resource. * * @return array */ protected function getRenderableFields(): array { $fields = []; if ($this->resource instanceof Model) { foreach ($this->schema()->fields() as $field) { if ($field instanceof RenderableField && $field->shouldRender($this->query)) { $fields[$field->getKey()] = $field->render($this->resource); } } } return $fields; } /** * Get the direct relations for the resource. * * @return array */ protected function getDirectRelations(): array { $relations = []; foreach ($this->schema()->allowedIncludes() as $allowedInclude) { $relationSchema = $allowedInclude->schema(); if ($allowedInclude->isDirectRelation() && $relationSchema instanceof EloquentSchema) { $relation = $this->whenLoaded($allowedInclude->path()); $relations[$allowedInclude->path()] = $relation instanceof Collection ? $relationSchema->collection($relation, $this->query) : $relationSchema->resource($relation, $this->query); } } return $relations; } /** * Get the allowed pivots for the resource. * * @return array */ protected function getAllowedPivots(): array { $pivots = []; $schema = $this->schema(); if ($schema instanceof InteractsWithPivots) { foreach ($schema->allowedPivots() as $allowedPivot) { /** @var EloquentSchema $pivotSchema */ $pivotSchema = $allowedPivot->schema(); $pivot = $this->whenLoaded($allowedPivot->path()); $pivots[$allowedPivot->path()] = $pivotSchema->resource($pivot, $this->query); } } return $pivots; } /** * Get the resource schema. * * @return Schema */ abstract protected function schema(): Schema; }