Laravel Go✓ Human-reviewed

Stable, collision-safe slugs in Laravel Go

Adapt slug generation to an application service with explicit normalization, retained existing slugs, database-enforced uniqueness, and bounded transactional retry behavior.

Approved 2026-12-04T12:03:48.748531Z · Submission 25

Stable, collision-safe slugs in Laravel Go

Pinned source and goal

Study Spatie's MIT-licensed spatie/laravel-sluggable 6.0.1 at f1e6344e16dd3dbbcc60ffd853aec58e721d33d5, focusing on src/HasSlug.php, src/SlugOptions.php, src/Actions/GenerateSlugAction.php, and tests. Keep upstream MIT notices if adapting source. Target: Laravel Go 56c7fd3691dc4ccb636119308d6fcf919e308518, module go.laravel.com.

Check get-recipe's current port catalog; if it lists a Go port, verify and offer that option before choosing to inline. This alternative handles one model's title-based slugs, update policy, and scoped collision behavior in an app-owned write path. It does not recreate translatable/self-healing/UUID APIs or redirect history. It has not been executed in your app; validate it with the checklist below.

Understand what must stay stable

The PHP package hooks create/update, allows manual overrides and skip/prevent rules, excludes the current record from collision checks, and uses language-aware Str::slug. Default uniqueness removes global scopes and reserves deleted records too; extra scope callbacks are explicit. Numeric suffixes start at 1. Source text is truncated before slugification, so final suffix length needs its own application check.

At this Go baseline str.Slug preserves accented Unicode letters (Café becomes café), unlike PHP's default English transliteration (cafe). The source also has special separator/dictionary behavior that a native helper may not mirror. A convenient helper is not proof of PHP-compatible URL generation.

Go adaptation

  1. Inventory the actual source model's options, overrides, write paths, route lookup, existing URLs, and redirect rules. Keep imported slug values unchanged. Decide whether title changes regenerate slugs; stable-on-update is a deliberate app choice, not the PHP package default.
  2. Add assignment at the existing application creation/update owner, shared with jobs/imports. Never accept an arbitrary tenant scope or column name from input. Preserve authorization separately from uniqueness.
  3. Choose native Unicode normalization or a verified transliteration pipeline matching a fixture corpus. Native normalization is only:
import "go.laravel.com/str"

base := str.Slug(title)

The two lines are a fragment, not a full Go file. str.ASCII can be evaluated when ASCII output is desired, but do not claim it exactly matches PHP's language-specific transliteration without comparison. Reject empty output or use a documented approved fallback. Truncate by rune/character policy with suffix room, never by arbitrary UTF-8 byte slicing.

  1. Define global versus tenant-scoped uniqueness and make route lookup use the same scope. Decide deleted-row reservation and restore behavior. Audit existing data before planning a matching database unique constraint; do not add a migration without reviewing its effect on real rows.
  2. Generate candidates from one original base: hello-world, hello-world-1, etc., with a bounded attempt count. Exclude self on update. Reserve room for the largest permitted suffix, and preserve manual-override validation rather than silently changing the user's explicit slug.
  3. Enforce uniqueness in the database. Two requests can both pass an existence check. Retry the whole write transaction only on the specific slug constraint's unique violation. PostgreSQL transactions are aborted after such a violation, so continuing inside the same callback is incorrect. Do not mistake an unrelated unique constraint or connection failure for a slug collision.
  4. Use db.Transaction with the callback's transaction-bound context for all participating reads/writes. No child goroutine may outlive that context. Bound retries and return the documented conflict response if exhausted; keep nonrepeatable external side effects outside retried callbacks.

Acceptance checklist

  • Repeated Hello World creates the expected base and numeric variants, with self excluded on update and any suffix-hole policy defined.
  • Compare PHP and Go on Café, a@b, underscores, combining characters, non-Latin, punctuation-only, and emoji-only inputs. Record differences instead of rewriting old slugs.
  • Boundary lengths and suffixes crossing 9 to 10 fit the column and remain valid UTF-8.
  • Explicit overrides, title edits, same-title records in different tenants, soft deletion, and restore obey the declared contract.
  • Run two creates on independent real-database connections. No duplicate scoped slug commits, and bounded retry/conflict behavior is observable.
  • Force an unrelated unique violation and assert it is not retried. Verify old URLs and tenant-aware route isolation remain correct.

Run gofmt, go vet ./..., go test ./..., and go test -race ./..., plus production-driver concurrency integration tests. The race detector does not prove SQL uniqueness. Record the Go/framework/normalizer versions and decisive test output with the change.