Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including Relationships #147

Open
Hesesses opened this issue Oct 24, 2019 · 2 comments
Open

Including Relationships #147

Hesesses opened this issue Oct 24, 2019 · 2 comments

Comments

@Hesesses
Copy link

Hello,

I have Transformers:

class ModelTransformer extends Transformer
{
    protected $load = [
        "organisation" => OrganisationTransformer::class
    ];

and

class OrganisationTransformer extends Transformer
{
    protected $relations = [
        'contact',
        'AnotherRelation' => AnotherRelationTransformer::class,
    ];

    protected $load = [];

    public function transform(Organisation $organisation)
    {
        return [
            'id'    => (int) $organisation->id,
            'name'  => (string) $organisation->name,
        ];
    }

    public function includeContact(Organisation $organisation)
    {
        return Transformation::make($organisation, OrganisationContactTransformer::class)->with('mailingAddress')->transform();
    }
}

Get request to /organisation?with=contact works
Get request to /model?with=organisation works
Get request to /model?with=organisation.AnotherRelation works
Get request to /model?with=organisation.contact doesnt work

Any ideas why with=organisation.contact relation is not working

@flugg
Copy link
Owner

flugg commented Oct 25, 2019

Hey, not sure, but I believe it has something to do with how the relations are resolved recursively. I'll see if I can replicate your scenario.

However, I'm not sure I understand why you're using an includeContact method to manually perform the transformation. Wouldn't

protected $relations = [
        'contact' => OrganisationContactTransformer::class,
        'AnotherRelation' => AnotherRelationTransformer::class,
];

And add 'mailingAddress' to OrganisationContactTransformer's $load property do the same?

@Hesesses
Copy link
Author

Hesesses commented Oct 25, 2019

Its because contact is not a real relation, Organisation has contact fields, but those are normally not needed. so those would only be returned when needed (with=contact)

The errors message is:

{
"status":422,
"success":false,
"error":{
    "code":"relation_not_found",
    "message":null
  }
}

Illuminate\Database\Eloquent\RelationNotFoundException: Call to undefined relationship [contact] on model [App\Organisation].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants