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

Select fields in included table problem #175

Closed
Trixpua opened this issue Feb 25, 2019 · 7 comments
Closed

Select fields in included table problem #175

Trixpua opened this issue Feb 25, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@Trixpua
Copy link

Trixpua commented Feb 25, 2019

If I make a request and include a relation table and specific to return just some fields, it return empty.

GET /categories?include=products
// returns categories with all data in products relation

GET /categories?include=products&fields[products]=name
// returns categories with a empty array in products relation

GET /categories?include=products&fields[products]=name,description
// returns categories with a empty array in products relation

But if I add the relation id column it returns all fields correct

GET /categories?include=products&fields[products]=category_id,name
// returns categories with a category_id and name array in products relation

GET /categories?include=products&fields[products]=category_id,name,description
// returns categories with a category_id, name and description array in products relation

Is necessary to add the relation id to this work or I am making something wrong?

@thebrubaker
Copy link

Yes if you do not include the foreign keys for how two tables are joined, then eloquent can't load that relationship, so I imagine you must include those fields necessary for joining two relations.

@ce-brex
Copy link
Contributor

ce-brex commented Mar 11, 2019

While you do need to include the foreign keys so eloquent can load it properly, this really seems like unnecessary information to have to pass. Seems like query builder should be able to figure this out on it's own and add it when needed.

@Trixpua
Copy link
Author

Trixpua commented Apr 11, 2019

The documention says I don't need it:

Selecting fields for included models works the same way. This is especially useful when including entire relationships when you only need a couple of columns. Consider the following example:

GET /posts?include=author&fields[author]=name
All posts will be fetched including only the name of the author.

But it dosen't work as example, need to add the 'author_id' and 'name' to work properly
GET /posts?include=author&fields[author]=author_id,name

I am newbie in laravel, sorry if I am talking bullshit, but as @ce-brex said, I think query builder should be able to figure out the relations keys without need to declare it in each request.

@AlexVanderbist
Copy link
Member

AlexVanderbist commented Apr 16, 2019

But it doesn't work as example

Ironically, I have never tested the examples 🙈- I'll update the readme.

I think query builder should be able to figure out the relations keys without need to declare it in each request.

I agree. However, we haven't figured out a good solution yet that works for all Eloquent relationships... Feel free to PR any idea that you guys might have. In the meantime I'll leave this issue open

@AlexVanderbist AlexVanderbist added the bug Something isn't working label Apr 16, 2019
@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

@DHSprout
Copy link

DHSprout commented Jan 3, 2021

I am facing same issue. Could you please suggest any solution for this?

@champak-7span
Copy link

"spatie/laravel-query-builder": "^3.3" in this version working well

But Custom field is not getting in the spatie/laravel-query-builder": "^5.2

alipadron added a commit to alipadron/laravel-query-builder that referenced this issue Jun 14, 2024
freekmurze added a commit that referenced this issue Jun 14, 2024
Add issue #175 link in selecting-fields.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants