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

Include eloquent model on included #40

Closed
tylergets opened this issue Feb 17, 2017 · 7 comments
Closed

Include eloquent model on included #40

tylergets opened this issue Feb 17, 2017 · 7 comments
Labels

Comments

@tylergets
Copy link

Hey guys, ive been struggling with these for an hour or so now. I have my searcher returning my "Tracks" model, with its tag IDs included. It's not including the tag data, such as its name, in the response. Is this possible?

This is all I am doing:

    return [
            'tags' => [
                self::DATA         => $resource->tags,
            ]
        ];
@dtr0yan
Copy link
Contributor

dtr0yan commented Feb 17, 2017

Hi @tylergets !
Sure it's possible. You just need to use include GET parameter and add allowedIncludePaths property to your Request class

@dtr0yan
Copy link
Contributor

dtr0yan commented Feb 17, 2017

So it will be something like http://yourhost.com/api/v1/tracks?included=tags

@lindyhopchris
Copy link
Member

Hey @tylergets

As @dmitry-php says, in your request object for tracks add $allowedIncludePaths = ['tags']. Then in the request from the client you need to include an ?include=tags as per the JSON API spec here:
http://jsonapi.org/format/#fetching-includes

If you want to set default include paths that are used if the client does not specify any, then implement the following method on your tracks Schema:
https://github.com/neomerx/json-api/blob/master/src/Schema/SchemaProvider.php#L227

E.g. to always include tags if the client doesn't specify any include paths via the query param:

public function getIncludePaths()
{
    return ['tags'];
}

@tylergets
Copy link
Author

Thanks guy, this got me through my initial problem. Now question for you @lindyhopchris, setting the getIncludePaths method works when its the main resource, is it possible to include the tags models when hitting for example, a "Playlist" endpoint that returns the tracks?

@jstoone
Copy link
Contributor

jstoone commented Feb 20, 2017

@tylergets yes, let's say you're querying GET /api/v1/playlists/1/tracks?include=tags, you should add tags to your playlists resource, just as you did on your tracks resource.

Hope it helps!

@lindyhopchris
Copy link
Member

@tylergets the include path is always relative to the main resource(s). So for playlists, the include path would be tracks.tags

@lindyhopchris
Copy link
Member

Closing this as it's quite old now and I think the questions were answered. Feel free to open a new issue if needed.

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

No branches or pull requests

4 participants