Selecting fields | laravel-query-builder-go | Spatie       

 SPATIE  

laravel-query-builder-go
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-query-builder-go](https://spatie.be/docs/laravel-query-builder-go/v1)  Features  Selecting fields

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-query-builder-go/v1) 

  Selecting fields    
- [ Requirements ](https://spatie.be/docs/laravel-query-builder-go/v1/requirements)
- [ Introduction ](https://spatie.be/docs/laravel-query-builder-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-query-builder-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-query-builder-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/laravel-query-builder-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-query-builder-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-query-builder-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-query-builder-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-query-builder-go/v1/about-us)

Features
--------

- [ Filtering ](https://spatie.be/docs/laravel-query-builder-go/v1/features/filtering)
- [ Sorting ](https://spatie.be/docs/laravel-query-builder-go/v1/features/sorting)
- [ Including relationships ](https://spatie.be/docs/laravel-query-builder-go/v1/features/including-relationships)
- [ Selecting fields ](https://spatie.be/docs/laravel-query-builder-go/v1/features/selecting-fields)

Advanced usage
--------------

- [ Extending query builder ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/extending-query-builder)
- [ Pagination ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/pagination)
- [ Multi value delimiter ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/multi-value-delimiter)
- [ Front-end implementation ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/front-end-implementation)
- [ Differences from PHP ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/port-differences)

 Selecting fields
================

###  On this page 

1. [ Basic usage ](#content-basic-usage)
2. [ Disallowed fields/selects ](#content-disallowed-fieldsselects)
3. [ Selecting fields for included relations ](#content-selecting-fields-for-included-relations)

Sometimes you'll want to fetch only a couple fields to reduce the overall size of your SQL query. Specify allowed fields with `AllowedFields` and request them with the `fields` query parameter.

Basic usage
-----------------------------------------------------------------------------------------

The following example fetches only users' ID and name:

```
// GET /users?fields[users]=id,name
rows, err := querybuilder.For(model.Query[User](), input).
    AllowedFields("users", "id", "name").
    Select(r)
if err != nil {
    return web.Fail(err)
}
type UserRow struct {
    ID int64 `json:"id"`
    Name string `json:"name"`
}
users, err := rows.Get[UserRow](r)
```

The first `AllowedFields` argument identifies the root request table key. Allowed columns can be unqualified or prefixed with that key. Unqualified requests such as `fields=id,name` use the same allowlist. `SnakeCaseFields` converts requested `firstName` to `first_name` before checking it.

With no requested fields, `Select` selects every root column. An allowlist controls which fields the client can request, not which fields your endpoint always returns. Use a response DTO or model JSON tags to hide sensitive data.

Laravel Go's `model.Builder.Select` changes the query type to `*db.Query`. Consequently sparse reads return row DTOs rather than hydrated models. `Get`, `First`, `Count`, `Paginate` and `Build` return `ErrModelFields` when sparse fields are requested. They never silently ignore a field selection.

For an explicit database connection, bind the execution context with `conn.Context(r)` and call `rows.On(conn)` when needed. The native `model.Builder.Select` does not transfer a model builder's `.On(conn)` setting to its returned `*db.Query`. Use model reads when you need native model connection and relationship behavior without this conversion.

Disallowed fields/selects
---------------------------------------------------------------------------------------------------------------------------------

Selecting a field outside `AllowedFields` returns a `*QueryError` with `Kind == "fields"`. No request value becomes raw SQL. Unrecognized root field names fail before extension callbacks run.

Unsupported nested values are skipped while valid siblings retain their table mapping. URL arrays accept scalar values. For explicit body input, pass decoded maps and arrays to `RequestData`.

Selecting fields for included relations
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Laravel Go v0.1.0 cannot hydrate models or eager-loaded relationships from partial column projections. Related `fields[posts]` input returns `ErrRelatedFields`. `Select` with requested includes returns `ErrSelectIncludes` rather than dropping the includes. Native loads already present on the subject are not carried into row reads either.

Return complete models through `Get`, then map their loaded relationships into response DTOs when you only need a subset of JSON attributes. This reduces response size but does not reduce selected SQL columns. PHP's relation-name casing and relation table-name conversion settings do not apply to this unsupported sparse-relationship path.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
