Skip to content

Commit ed706e8

Browse files
authored
Merge pull request #1008 from amyavari/docs/select-include-fields
Docs: clarify selecting fields for included relations
2 parents 9b48223 + 86ba5ca commit ed706e8

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

config/query-builder.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@
6262
'convert_relation_names_to_snake_case_plural' => true,
6363

6464
/*
65-
* By default, the package expects relationship names to be snake case plural when using fields[relationship].
66-
* For example, fetching the id and name for a userOwner relation would look like this:
67-
* GET /users?fields[user_owner]=id,name
65+
* This is an alternative to the previous option if you don't want to use default snake case plural for fields[relationship].
66+
* It resolves the table name for the related model using the Laravel model class and, based on your chosen strategy,
67+
* matches it with the fields[relationship] provided in the request.
6868
*
69-
* Set this to one of `snake_case`, `camelCase` or `none` if you want to enable table name resolution in addition to the relation name resolution
70-
* GET /users?include=topOrders&fields[orders]=id,name
69+
* Set this to one of `snake_case`, `camelCase` or `none` if you want to enable table name resolution in addition to the relation name resolution.
70+
* `snake_case` => Matches table names like 'topOrders' to `fields[top_orders]`
71+
* `camelCase` => Matches table names like 'top_orders' to 'fields[topOrders]'
72+
* `none` => Uses the exact table name
7173
*/
7274
'convert_relation_table_name_strategy' => false,
7375

docs/features/including-relationships.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ QueryBuilder::for(User::class)
169169

170170
## Selecting included fields
171171

172-
You can select only some fields to be included using the [`allowedFields` method on the query builder](https://spatie.be/docs/laravel-query-builder/v5/features/selecting-fields/).
172+
You can select only some fields to be included using the [`allowedFields` method on the query builder](https://spatie.be/docs/laravel-query-builder/v6/features/selecting-fields/).
173173

174174
⚠️ `allowedFields` must be called before `allowedIncludes`. Otherwise the query builder wont know what fields to include for the requested includes and an exception will be thrown.
175175

docs/features/selecting-fields.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ $users = QueryBuilder::for(User::class)
4242
Selecting fields for included models works the same way. This is especially useful when you only need a couple of columns from an included relationship. Consider the following example:
4343

4444
```php
45-
GET /posts?include=author&fields[author]=id,name
45+
GET /posts?include=author&fields[authors]=id,name
4646

4747
QueryBuilder::for(Post::class)
48-
->allowedFields('author.id', 'author.name')
48+
->allowedFields('authors.id', 'authors.name')
4949
->allowedIncludes('author');
5050

5151
// All posts will be fetched including _only_ the name of the author.
5252
```
53+
⚠️ **Note:** In `allowedFields`, you must always use the _snake case plural_ of your relation name. If you want to change this behavior, you can change the settings in the [configuration file](https://spatie.be/docs/laravel-query-builder/v6/installation-setup)
5354

5455
⚠️ Keep in mind that the fields query will completely override the `SELECT` part of the query. This means that you'll need to manually specify any columns required for Eloquent relationships to work, in the above example `author.id`. See issue [#175](https://github.com/spatie/laravel-query-builder/issues/175) as well.
5556

docs/installation-setup.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ composer require spatie/laravel-query-builder
1212
The package will automatically register its service provider.
1313

1414
You can optionally publish the config file with:
15+
1516
```bash
1617
php artisan vendor:publish --provider="Spatie\QueryBuilder\QueryBuilderServiceProvider" --tag="query-builder-config"
1718
```
@@ -72,12 +73,33 @@ return [
7273
/*
7374
* By default, the package expects relationship names to be snake case plural when using fields[relationship].
7475
* For example, fetching the id and name for a userOwner relation would look like this:
75-
* GET /users?fields[user_owner]=id,name
76+
* GET /users?include=userOwner&fields[user_owners]=id,name
7677
*
7778
* Set this to `false` if you don't want that and keep the requested relationship names as-is and allows you to
7879
* request the fields using a camelCase relationship name:
79-
* GET /users?fields[userOwner]=id,name
80+
* GET /users?include=userOwner&fields[userOwner]=id,name
8081
*/
8182
'convert_relation_names_to_snake_case_plural' => true,
83+
84+
/*
85+
* This is an alternative to the previous option if you don't want to use default snake case plural for fields[relationship].
86+
* It resolves the table name for the related model using the Laravel model class and, based on your chosen strategy,
87+
* matches it with the fields[relationship] provided in the request.
88+
*
89+
* Set this to one of `snake_case`, `camelCase` or `none` if you want to enable table name resolution in addition to the relation name resolution.
90+
* `snake_case` => Matches table names like 'topOrders' to `fields[top_orders]`
91+
* `camelCase` => Matches table names like 'top_orders' to 'fields[topOrders]'
92+
* `none` => Uses the exact table name
93+
*/
94+
'convert_relation_table_name_strategy' => false,
95+
96+
/*
97+
* By default, the package expects the field names to match the database names
98+
* For example, fetching the field named firstName would look like this:
99+
* GET /users?fields=firstName
100+
*
101+
* Set this to `true` if you want to convert the firstName into first_name for the underlying query
102+
*/
103+
'convert_field_names_to_snake_case' => false,
82104
];
83105
```

0 commit comments

Comments
 (0)