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

AllowedFields not working #831

Closed sergyu91ma closed 1 year ago

sergyu91ma commented 1 year ago

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);

}`

kosarinin commented 1 year ago

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