Laravel Go✓ Human-reviewed

One explicit relationship join in Laravel Go

Extract one fixed relationship join with context-bound database access, explicit tenant/deletion predicates, nullable projections and stable parent pagination.

Approved 2026-12-04T14:14:42.693755Z · Submission 80

One explicit relationship join in Laravel Go

Scope

Replace one authorized listing's package-backed belongsTo join with a fixed app-owned query. Preserve custom foreign/owner keys, join type, parent cardinality, selected fields and response envelope. Nested, polymorphic, pivot and to-many joins, aggregate sorting and a general relationship dispatcher are excluded. Keep the upstream MIT notice when adapting source.

At the time of writing, the Package Port catalog listed gaps for relationship-name joins in Go and Rust. Check get-recipe's current port catalog before choosing to inline. Ordinary table joins may already solve the selected caller. This guidance has not been exercised in your app; verify it with the checks below.

Source behaviour

In the inspected src/Mixins/JoinRelationship.php, relationship names resolve into join operations with alias/deduplication handling. A query without an explicit selection gets a base-table-only default selection. Joining is not eager-loading a relationship.

src/Mixins/RelationshipsExtraMethods.php::performJoinForEloquentPowerJoinsForBelongsTo connects the parent foreign key to the related owner key, adding applicable soft-delete and supported extra relationship conditions unless disabled. src/PowerJoinClause.php exposes withGlobalScopes for explicit callback use; arbitrary joined-model scopes are not a guarantee of a plain join. Compare the real source-backed SQL and bindings, not just a relationship's name.

Implementation

  1. Inventory the endpoint's relationship keys, callbacks, aliases, scopes, soft-delete overrides, columns, sort and pagination. Carry trusted actor/tenant identity and the existing request context into its query path. A handler's authenticated user is not automatically a SQL tenant predicate.
  2. Use the app's existing go.laravel.com/db query path and context-bound connection to construct one fixed join. Bind data values; keep table/column/alias/order expressions app-owned. Do not copy a PHP relationship resolver into Go or allow request input to select a relationship name.
  3. Establish uniqueness of the related owner key under the join. For the chosen left-join contract, put related visibility/tenant/deletion predicates in ON, preserving parents without an eligible author. A WHERE predicate on the joined table can remove them. Preserve an existing inner join when eligibility is required to return the parent. Adding a policy absent upstream is a deliberate contract change, even if desirable.
  4. Select parent columns and aliased related fields explicitly. Scan nullable author names using the driver's supported nullable type or an optional response field; don't conflate null with an empty name. Avoid SELECT * collisions and don't treat a joined result as a fully loaded relationship model. Use authorized EXISTS instead if the caller needs existence only, documenting the deliberate implementation substitution.
  5. Preserve stable ordering, parent-key tie-breakers, page size and totals. No write or schema migration is required. If the existing count/page contract needs a shared snapshot, keep its isolation policy; in a db.Transaction callback every query uses the supplied txctx, not the outer context. Pass cancellation to all database calls. Scope any cache by tenant, permissions, filters and page; retain invalidation when related values change.

Illustrative PostgreSQL query only. It assumes unique authors.id, posts.author_id, and tenant_id/deleted_at columns on both tables. The policy retains visible posts and redacts ineligible author details. $1 is a bound trusted tenant, not an identifier; other application scopes remain to be applied:

SELECT p.id, p.title, a.name AS author_name
FROM posts AS p
LEFT JOIN authors AS a
  ON a.id = p.author_id
 AND a.tenant_id = p.tenant_id
 AND a.deleted_at IS NULL
WHERE p.tenant_id = $1 AND p.deleted_at IS NULL
ORDER BY p.id;

Match the real driver's placeholder/scanning API; this is not a framework method signature. Return only the existing endpoint's allowed fields.

Acceptance checklist

  • Tenant 1 has two posts sharing a live author, one null-author post, one deleted-author post and one pointing to tenant 2's author. Add a deleted parent and another tenant's parent. Expect five visible parent IDs exactly once each under the illustrated left-join policy; the last three author names are null, and deleted/foreign parents are absent.
  • Compare the source-backed endpoint's actual inner/left semantics, selected values, filters and total count. Verify explicitly used withTrashed/onlyTrashed and related scope callbacks separately.
  • Repeat related sort values across two pages; assert stable ordering, disjoint IDs and correct totals. Verify the production query plan and bounded query count, not an N+1 follow-up fetch.
  • An unauthorized request returns no data. Foreign-tenant related data and cached projections remain inaccessible. Null is distinct from an eligible author's empty string.
  • Cancel the request and confirm database work ends according to the installed driver's semantics. If count and page share a transaction, exercise the actual transaction context.

Run gofmt, go vet ./..., go test ./..., and the app's production-driver listing/authorization tests. Record versions, decisive output and deliberate differences with the change. Search remaining package callers before dependency removal.

Sources

  • MIT reference source: src/Mixins/JoinRelationship.php, src/Mixins/RelationshipsExtraMethods.php, src/PowerJoinClause.php, tests/JoinWithGlobalScopeTest.php, tests/SoftDeletesTest.php, and tests/JoinRelationshipWithJoinTypesTest.php. Retain the upstream license notice for adapted code.
  • Framework source checked: db/tx.go; use the installed query/scanning APIs and compare the metadata to the real app's module revision.
  • Port options: check get-recipe's current port catalog (sourced from Package Port) at implementation time.