Laravel Rust✓ Human-reviewed

One tag-filtered listing in Laravel Rust

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

Approved 2026-12-04T15:00:08.371934Z · Submission 106

One tag-filtered listing in Laravel Rust

Scope

Replace one withAnyTags read with an application-owned existence query over the existing tags/polymorphic pivot tables. This recipe accepts two already-resolved tag IDs and one non-null tag type; it does not replace translated name/slug resolution, tag creation, sorting, attach/detach/sync, lifecycle hooks or all-tags/without-tags scopes. Preserve Spatie's MIT notice when adapting source. At the time of writing the port catalog listed an official Rust port and a community alternative; check get-recipe's current port catalog before choosing extraction. This guidance has not been exercised in your app; verify it below.

Source behaviour

src/HasTags.php implements scopeWithAnyTags by converting inputs to tags and applying whereHas with their IDs. tags() is a configured morph-to-many relation with ordering. scopeWithAllTags adds one existence condition per converted tag, so it is not interchangeable with any-tags. src/Tag.php::findManyFromString filters exact type and groups locale-specific name/slug alternatives; a null type there means null, not every type. tests/HasTagsScopesTest.php checks any/all, nonexistent names and type selection.

The query below deliberately moves the input boundary to resolved IDs, uses a single named non-null type and adds application-specific tenant/deletion policies. It does not implement the string-input API. Preserve that resolution separately if callers still submit names or slugs.

Implementation

  1. Inventory the selected scope and its consumers, configured table/key names, morph map, custom Tag model/global scopes and soft deletion. Capture exact ordered row/count fixtures. Keep writes and other scope calls package-backed until separately replaced.
  2. Authorize the listing, derive tenant from the actor, and resolve tag IDs through the app's allowed tag/type policy. A tag ID, type or morph class sent by a request is not authority. Default package tags can be shared; do not assume a tenant column exists on them. Missing/forbidden IDs must follow the endpoint's chosen disclosure policy.
  3. Bind values through the installed database layer. Use a literal query and fixed identifiers. The example's two-ID selection is deliberate; for an arbitrary bounded list use a parameterized list predicate supported by the installed adapter, never interpolated request SQL. Empty any-tags must match no parents; do not accidentally omit the filter. Deduplicate IDs to bound work, not to repair parent duplication.
  4. Preserve global scopes explicitly when using raw SQL. Resolve the actual persisted morph alias from trusted configuration, not the Rust model name. EXISTS preserves one row per parent even with multiple matching pivots. Apply the same predicate before counting/pagination, use deterministic order, and retain parent authorization/projections.
  5. This read needs no write transaction or new schema/cache. If the endpoint requires count/page snapshot consistency, retain its existing isolation policy. Check the actual driver's plan and existing pivot indexes without automatically migrating production. Keep deletion/sync behavior unchanged elsewhere.

Complete PostgreSQL query for the illustrated fixed schema. Bind tenant, trusted morph alias, two resolved IDs and non-null type in that order. posts, tags and taggables are examples to reconcile with the app, never request-provided identifiers:

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;

The inspected typed database connection exposes query(...).bind(...) with native SQLx bindings. Keep binding at that boundary; this SQL is not a framework handler and does not authorize requests by itself.

Acceptance checklist

  • With selected IDs 11/12 of type topic, return a parent linked to either ID exactly once, including one linked to both; exclude a parent linked only to a different type. Compare the exact ordered IDs and count to the package-backed path.
  • Seed the same numeric parent ID under a different morph alias, another tenant's post, a deleted post, an unrelated tag and a parent without tags. None leaks through. Include missing tags, duplicate selected IDs and empty selection.
  • Reconcile a name matching another tag's slug, locale differences, null type versus non-null type and custom scopes before changing any string-input caller. Do not pretend this query preserves those APIs.
  • Verify authorization, bounded request size, count/page consistency, binding of hostile strings, empty result serialization and the production driver's plan. Writes, queued tags, deletion hooks and tag order remain intact.
  • Search trait/scopes and cargo tree/remaining PHP consumers before removing dependencies. Run destination request/database tests and record versions, exact expected rows and deliberate differences.

Sources

  • MIT reference: src/HasTags.php, src/Tag.php, tests/HasTagsScopesTest.php, LICENSE.md.
  • Framework source checked: crates/database/src/db/connection.rs and crates/database/src/db/builder/execute.rs. Reconcile with the app's lockfile and database.
  • Port options: check get-recipe's current port catalog (sourced from Package Port) at implementation time.