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

Selecting fields for included relations #959

Open JaviLerma opened 1 month ago

JaviLerma commented 1 month ago

Greetings from Argentina! The Official docs says:

QueryBuilder::for(Post::class)  
    ->allowedFields('author.id', 'author.name')  
    ->allowedIncludes('author')  
    ->get();

GET /posts?include=author&fields[author]=id,name

Return all post but only id and name of the author, but it doesn't work. It fetches all the authors fields.

Relationships

User model:

public function posts(): HasMany
    {
        return $this->hasMany(Post::class, 'author_id');
    }}

Post model:

 public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'author_id');
    }

I am doing something wrong or its a bug? Thanks

luizmagao commented 1 month ago

Estou passando pelo mesmo problema, não sei como resolver ainda.