spatie / laravel-query-builder

Easily build Eloquent queries from API requests

Home Page:https://spatie.be/docs/laravel-query-builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Mina-R-Meshriky opened this issue · comments

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.

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

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.

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.

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?