laravel-query-builder
laravel-query-builder copied to clipboard
feat: Automatically include 'id' field in related table queries
(this may be also related to issue #959
Automatically Include 'id' Field in Related Table Queries
Description
This PR modifies the getRequestedFieldsForRelatedTable method to automatically include the 'id' field when querying related tables. This enhancement improves user experience by reducing the need for redundant specifications in API requests.
Motivation and Context
When working with related tables, the 'id' field is often crucial for maintaining relationships and performing subsequent operations. However, requiring users to explicitly include 'id' in every request (e.g., fields[carModel]=slug,id) can be cumbersome and error-prone. This change aims to streamline the API usage while ensuring that essential data is always available.
Hi @inmass, thanks for another interesting contribution! I agree that it's not the best DX to include the relevant id columns on included fields all the time. However, I never found a good fix for this because there's a bunch of edge cases where it's not as simple as adding a select on id. For example, some people use uuid as a key, while others use custom foreign key columns like related_id. There are also special relationship types, such as polymorphic relations, which require more than one column to clarify the relationship. Maybe I see the issue as more complicated than it actually is, but let's get some test cases going first to see where this breaks :)
Dear contributor,
because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it.