Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Automatically include 'id' field in related table queries #973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/features/selecting-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@ $users = QueryBuilder::for(User::class)
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:

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

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

// All posts will be fetched including _only_ the name of the author.
// All posts will be fetched including _only_ the id a nd name of the author (id is always selected).
```
⚠️ By default, relationship names are expected to be in plural form (e.g., 'authors' instead of 'author' | 'postComments' instead of 'postComment'). This applies to both table names and relationship names in queries.

If you prefer to use singular names, you need to publish the query-builder config file and set the `convert_relation_names_to_snake_case_plural` option to `false`:

```php
php artisan vendor:publish --provider="Spatie\QueryBuilder\QueryBuilderServiceProvider" --tag="query-builder-config"
```

```php
'convert_relation_names_to_snake_case_plural' => false,
```

⚠️ 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.
Expand Down
5 changes: 5 additions & 0 deletions src/Concerns/AddsFieldsToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function getRequestedFieldsForRelatedTable(string $relation): array
throw new UnknownIncludedFieldsQuery($fields);
}

// add id to fields
if (! in_array('id', $fields)) {
$fields[] = 'id';
}

return $fields;
}

Expand Down