One explicit relationship join in Laravel Rust
Scope
Replace one package-backed belongsTo join used by an authorized listing with an app-owned query. Preserve its foreign/owner keys, inner-versus-left semantics, row cardinality, selected fields and response envelope. Exclude polymorphic, pivot, nested and to-many joins, relationship-name dispatch, aggregate sorting and generic join discovery. Retain the package-backed path for callers outside this subset. Keep the upstream MIT notice when adapting source.
At the time of writing, the Package Port catalog listed gaps for relationship-name joins in Rust and Go. Check get-recipe's current port catalog before choosing to inline. Existing table-join support may be sufficient; a catalog gap is not proof that no alternative exists. This guidance has not been exercised in your app; verify it with the checks below.
Source behaviour
In the implementation linked under Sources, src/Mixins/JoinRelationship.php resolves a named relationship, handles aliases and duplicate joins, and supplies a base-table-only default selection when no explicit selection exists. It does not hydrate a related model just because it joined its table.
src/Mixins/RelationshipsExtraMethods.php::performJoinForEloquentPowerJoinsForBelongsTo joins the parent's foreign key to the related owner key. Unless extra conditions are disabled, it adds applicable soft-delete and supported relationship constraints to the join clause. PowerJoinClause::withGlobalScopes is an explicit callback API: do not assume arbitrary joined-model global scopes are automatically present. Its tests exercise explicit scope application, soft-delete overrides and join types. Capture the actual caller's SQL and bindings before replacing it.
Implementation
- Inventory the chosen model relationship, custom keys, callbacks, aliases,
withTrashed/onlyTrashed, global/tenant scopes, selected fields and order. Retain the authorized base query. Convert each relevant related-model policy into a fixed predicate; a plain table join does not apply its model scopes. Do not accept relationship, table, column, sort direction or SQL fragments from request input. - Use the installed query builder or the app's existing repository to express one fixed join. Bind values through the driver's parameter API. Prove that the owner key is unique under the join's conditions; otherwise this is not a one-row-per-parent relationship and needs a different plan. Never hide unexpected duplication with
DISTINCT. - For a left join that preserves parents without an eligible related row, keep related eligibility predicates in
ON. Moving a tenant/visibility condition intoWHEREcan silently remove those parents. If the old endpoint instead excludes such parents, retain its inner join or existence predicate. Any stronger visibility policy is a deliberate behaviour change to record, not presumed parity. - Select parent fields explicitly and alias each projected related field. Decode nullable related fields as optional values, not empty strings or zero IDs. Do not decode colliding
idcolumns into the parent model or mark a relationship as loaded. If only existence is needed, an authorizedEXISTSquery may be simpler than a join; that is a deliberate implementation substitution requiring equivalent fixtures. - Preserve deterministic ordering and pagination. Keep the existing unique parent-key tie-breaker, define null ordering for related-field sorts, and verify page totals. A read-only listing needs no new transaction; if count and page must represent one snapshot, use the app's existing production-driver transaction policy. Cache only with actor/tenant/filter/page-aware keys and existing invalidation rules.
Illustrative PostgreSQL query, not a complete endpoint. It assumes posts.author_id references unique authors.id, both tables use tenant_id/deleted_at, and the chosen policy preserves visible posts while hiding ineligible author details. $1 is the trusted tenant binding; no additional application scopes are assumed covered:
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;
Use the installed Rust driver's placeholder/binding convention, not textual replacement. Map author_name to an optional string; keep HTTP authorization and response mapping at their existing boundaries.
Acceptance checklist
- In tenant 1, create two posts sharing a live author, one null-author post, one deleted-author post and one referencing tenant 2's author. Add a deleted parent and a parent owned by tenant 2. The left-join policy above returns exactly five tenant-1 parents, both shared-author posts once each; null/deleted/foreign-author names are null. The deleted and foreign parents are absent.
- Compare source-backed IDs, projected values, bindings and totals for inner and left variants actually used. A missing author must not become an empty name. A deliberately changed visibility policy needs separate expected results.
- Add a related scope/callback and verify its exact replacement. Include soft-delete overrides if used; a test of the default path does not prove those overrides survive.
- Paginate records with repeated sort values across page boundaries. Assert no duplicates/missing IDs and correct totals; run
EXPLAINon the production driver and check bounded query count without an N+1 lookup. - Deny an unauthorized listing and isolate two tenants' cached results. Search remaining package calls before removing only this endpoint's dependency.
Run cargo fmt --all --check, cargo clippy --all-targets -- -D warnings, and the app's listing/authorization integration tests on its production driver. Record framework/driver versions, outputs and intentional differences with the change.
Sources
- MIT reference source:
src/Mixins/JoinRelationship.php,src/Mixins/RelationshipsExtraMethods.php,src/PowerJoinClause.php,tests/JoinWithGlobalScopeTest.php,tests/SoftDeletesTest.php, andtests/JoinRelationshipWithJoinTypesTest.php. PreserveLICENSE.mdattribution when adapting source. - Framework source checked:
crates/database/src/db/builder.rsanddb/neutral.rs; reconcile metadata with the app's lockfile. - Port options: check
get-recipe's current port catalog (sourced from Package Port) at implementation time.