dingo / dingo/api

Including custom data in transformer

Open
#1,096 13 comments 0 reactions 0 assignees View on GitHub
bug documentation
Dominant language
PHP
Stars
9.4k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hello there,

I have a repository that queries an API for data (rather than using a model) and returns either an object or a collection. I've been trying to include this data in a transformer for another model with limited success. Obviously I cannot us includes as this requires a model relationship, so for the moment I've hackishly grabbed my interface and checking whether a property exists on the model being transformed. My only issue is that the item is not rendering... Here's my "dodgy" code:

``` PHP
public function transform(Domain $domain)
{
$output = [
'id' => (int) $domain->id,
'client_id' => (int) $domain->client_id,
'url' => (string) $domain->url
];

if ($id = $domain->dns_id) {
$zone = app(ZoneRepositoryInterface::class);
$output['dns'] = $this->item($zone->getById($id), new ZoneTransformer);
}

return $output;
}
```

`$this->item($zone->getById($id), new ZoneTransformer)` appears to be the issue, it's calling the item successfully (can see object when debugging) but it's not outputting the `ZoneTransformer`, just a couple of `{}`.

Is there a better way of doing this? Any reason the item is not rendering?

Thanks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.