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

AllowedFields not working

sergyu91ma opened this issue · comments

Hello,

Versions i use:
"spatie/laravel-query-builder": "5.0.3",
"laravel/framework": "9.37.0",

Code:

$products = QueryBuilder::for(Product::class)->allowedFields( 'importer','name' )->limit(6)->get();

In telescope query for the above code is:
select * from products limit 6

which selects all fields and returns them and i don't want this of course. I want only importer and name to be selected.

Shouldn't query be (at least this is whats i saw in docs):
SELECT 'importer','name' FROM products LIMIT 6 ?

I debugged a little and got into this:

`
//File -> vendor/spatie/laravel-query-builder/src/Concerns/AddsFieldsToQuery.php
protected function addRequestedModelFieldsToQuery()
{
$modelTableName = $this->getModel()->getTable();

    $modelFields = $this->request->fields()->get($modelTableName);

    if (empty($modelFields)) {  // -> HERE modelFields is NULL
        return;
    }

    $prependedFields = $this->prependFieldsWithTableName($modelFields, $modelTableName);

    $this->select($prependedFields);

}`

@sergyu91ma Could you provide a curl of your HTTP request?