Laravel Go✓ Human-reviewed

One tag-filtered listing in Laravel Go

Extract one authorized any-tags existence query with explicit type, morph and tenant boundaries, without duplicating parent rows.

Approved 2026-12-04T15:00:07.80882Z · Submission 104

One tag-filtered listing in Laravel Go

Scope

Replace one withAnyTags listing with a parameterized existence query over existing tags and morph pivots. The bounded input is two already-resolved IDs and one non-null type. Exclude name/slug translation, tag creation, all-tags/without-tags, ordered relations, synchronization and deletion hooks. Preserve Spatie's MIT notice for adapted source. At the time of writing the port catalog listed an official Go port; check get-recipe's current port catalog before extraction. This guidance has not been exercised in your app; verify it below.

Source behaviour

src/HasTags.php::scopeWithAnyTags resolves inputs and uses one whereHas with tag IDs; scopeWithAllTags adds separate existence conditions. tags() uses configured morph/table/pivot classes and ordering. src/Tag.php::findManyFromString selects exact type and grouped locale-specific name/slug matches. In that lookup null type is not any type. tests/HasTagsScopesTest.php exercises any/all, unknown strings and typed tags.

The example deliberately accepts IDs rather than the package's string/Tag input API, fixes a non-null type, and adds an application tenant/deletion policy. Name/slug callers require their own compatible resolver before switching.

Implementation

  1. Inventory the chosen scope, model/global scopes, morph aliases, custom identifiers, soft deletion and output shape. Capture original ordered rows and pagination totals. Keep write paths and other relation consumers on the package until replaced independently.
  2. Authorize parent access and resolve IDs within the app's tag visibility/type policy. Derive tenant and morph alias from trusted actor/configuration, not query parameters. Shared upstream tags do not inherently have a tenant column. Apply the app's existing missing/forbidden policy without disclosing private tag names.
  3. Use the installed db layer or the app's established PostgreSQL adapter to bind values and propagate query/scan/context errors. The illustrated query has exactly two ID placeholders; use a verified builder for variable bounded lists, not string-built request SQL. Empty any-tags deliberately matches nothing, never an unrestricted listing.
  4. Preserve every applicable global scope explicitly. Existence filtering avoids multiplication when several selected tags match. Count using the same scoped predicate before pagination; retain deterministic order, explicit projections and empty-array responses. Do not load every parent into Go to filter tags.
  5. Keep the existing read isolation/cancellation policy and cache keys, including tenant, type and normalized selection. No writes, new cache or schema are needed. Check existing indexes and the actual query plan separately. Leave attach/detach/sync and their transaction policy untouched.

Complete PostgreSQL query for the fixed example schema, not a handler. Bind trusted tenant, morph alias, two authorized IDs and non-null type in order. Database identifiers are literals; reconcile them with the app, never request input:

SELECT p.id, p.title
FROM posts AS p
WHERE p.tenant_id = $1 AND p.deleted_at IS NULL
  AND EXISTS (
    SELECT 1 FROM taggables AS pt
    JOIN tags AS t ON t.id = pt.tag_id
    WHERE pt.taggable_id = p.id AND pt.taggable_type = $2
      AND t.id IN ($3, $4) AND t.type = $5
  )
ORDER BY p.id;

Use the placeholder dialect of the actual driver. Do not replace placeholders with formatted strings, and do not copy a PostgreSQL query into a MySQL-only adapter unchanged.

Acceptance checklist

  • IDs 11/12 of type topic match a parent with either tag and return a parent with both only once. Compare exact ordered IDs and counts against upstream; include duplicate input IDs, unknown IDs and empty selection.
  • A pivot for another morph alias with the same numeric parent ID must not match. Exclude other tenants, deleted parents, unrelated tags, wrong-type tags and untagged parents.
  • Capture locale/name/slug/null-type resolution fixtures before changing any string caller. This ID-only query is not their resolver. Check custom Tag scopes and configured pivot keys.
  • Exercise unauthorized requests, hostile bound values, cancellation, database errors, count/page boundaries, empty JSON arrays and the production driver's plan. Run go test ./... and relevant race tests; record toolchain/framework versions.
  • Search remaining scopes, relation writes and go list -m all/PHP dependency consumers before removing a package. Preserve lifecycle hooks and deliberately excluded behavior until separately replaced.

Sources

  • MIT reference: src/HasTags.php, src/Tag.php, tests/HasTagsScopesTest.php, LICENSE.md.
  • Framework source checked: database transaction/query boundary in db/tx.go; reconcile the app's adapter and placeholder dialect. This is an unversioned source checkout, not a claimed release.
  • Port options: check get-recipe's current port catalog (sourced from Package Port) at implementation time.