laravel-query-builder icon indicating copy to clipboard operation
laravel-query-builder copied to clipboard

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

Open Mina-R-Meshriky opened this issue 2 years ago • 2 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.

Mina-R-Meshriky avatar Aug 17 '22 17:08 Mina-R-Meshriky

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

maartenpaauw avatar Aug 26 '22 07:08 maartenpaauw

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.

Mina-R-Meshriky avatar Sep 01 '22 15:09 Mina-R-Meshriky

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.

spatie-bot avatar Jan 02 '23 11:01 spatie-bot

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?

mstanarevic avatar Mar 23 '23 14:03 mstanarevic