spatie / laravel-query-builder

Easily build Eloquent queries from API requests
https://spatie.be/docs/laravel-query-builder
MIT License
4.02k stars 395 forks source link

allowedIncludes and allowedFields in not standard named relation model #845

Closed paul-lestyo closed 1 year ago

paul-lestyo commented 1 year ago

in case, i have model Event::class who have column assign_by and assign_to refer Staff::class. I create function relations like this image

then when I try allowedFields column staff.id its didn't work, but when I use fileds[assign_by] that is not referred to any table name.

I try to dump variable $relation and $table in function getRequestedFieldsForRelatedTable() and get these result image

is it a bug or i am having mistake in my code? Thx

AlexVanderbist commented 1 year ago

Hey, can you share the full request URL and what your QueryBuilder code looks like in your controller, please? It'll be easier to identify whether this is a bug or a misunderstanding with the package :)

VapoFred78 commented 1 year ago

Hello, I would like to respond to this issue because I have the same problem. For example : products?fields[products]=id,name,brand_id&include=brand&sort=-products.id&paginate=15&filter[brand.name]=Power here: fields[products], products should be the name of the table, not the relationship, which is a shame. Similarly, &sort=-products.id, the sorting must be done with the name of the table, and not the relationship, whereas it would be simpler with the name of the relationship. On the other hand, &filter[brand.name] is done with the name of the relationship... The PR https://github.com/spatie/laravel-query-builder/pull/794 fixes these little problems. See line 52 of the AddsFieldsToQuery feature ;)

spatie-bot commented 1 year ago

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.