Skip to content

Is it possible to include nested relationships? #44

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

Closed
utf4 opened this issue Feb 25, 2017 · 3 comments
Closed

Is it possible to include nested relationships? #44

utf4 opened this issue Feb 25, 2017 · 3 comments
Labels

Comments

@utf4
Copy link

utf4 commented Feb 25, 2017

Is it possible to fetch nested relationship. lets say I am having three resource User, Roles, and Permissions. Permissions have reference of Roles and Roles have reference of Users. I want to Fetch a User along-with its all Permissions referenced against all of its Roles.

What I was trying to pass a query parameter http://myapi.com/v1/users?include=roles.permissions because in laravel nested relationships are fetched like this. This is giving me error of not allowed include.

I was looking if there is any work around, Otherwise what I have to do is:

  • Fetching User in first request.
  • Loop through all of its roles and send request to fetch their permissions one by one.

ps: I have considered users, roles, permissions example to make it simple. I am implementing this thing against some complex data models which do have hundreds of relationship records.

Thanks
Furqan Aziz

@GregPeden
Copy link
Contributor

HEY I was coming here to ask the same question!

In my case I have a model design for "address" which is used by polymorphic relationship to various other models. An address model has a city model relationship attached to it, which implies a "division" (ex. state) relationship, which implies a country relationship. And then various models have an "address" relationship to their own one-to-one address pairing. In my program, all of the cities in Canada and USA are already in the database (from UN database) and so a false city cannot be entered.

But... I'd like to be able to, say "select all buildings in Canada" via the API, as an extreme case example.

@lindyhopchris
Copy link
Member

Hi! As per the JSON API spec it is possible to request nested relationships:
http://jsonapi.org/format/#fetching-includes

Using dot notation in the include query param. So roles.permissions is correct. However you would need to allow that include path in your users Request class. I.e. make sure roles.permissions is in your $allowedIncludePaths on the Request class.

@lindyhopchris
Copy link
Member

Closing this as I don't think there's anything else needed... 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

3 participants