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

Allowed fields key should be the route name not the database table name #805

Closed Mina-R-Meshriky closed 1 year ago

Mina-R-Meshriky commented 2 years ago

When we call the following route /api/product-categories/1?fields[product-categories]=id,name it does not work and gives a 400 error.

but when we change the fields key to [product_categories] -which is the table name in the database- it works!

the same can be said about the fields, if a frontend dev. requests createdAt -which is the name in the resource file- And in the queryBuilder ->allowedFields(['id','name', 'createdAt', 'updatedAt']) it will get him an error. However, he has to request it as created_at and the queryBuilder has to be ->allowedFields(['id','name', 'created_at', 'updated_at'])which are again the names in the database.

maartenpaauw commented 2 years ago

If I understand you correctly you are looking for aliases. Check out the chapter "Content Filter Aliases" in the documentation: https://spatie.be/docs/laravel-query-builder/v5/features/filtering#content-filter-aliases

Mina-R-Meshriky commented 2 years ago

No this is not about the filters in the documentation. it is related to "sparse fields" or as in the documentation "selecting fields" https://spatie.be/docs/laravel-query-builder/v5/features/selecting-fields the route will only accept table names and table columns with no aliases.

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.

mstanarevic commented 1 year ago

No this is not about the filters in the documentation. it is related to "sparse fields" or as in the documentation "selecting fields" https://spatie.be/docs/laravel-query-builder/v5/features/selecting-fields the route will only accept table names and table columns with no aliases.

Hi @mina-R-Meshriky, did you find any solution to this?