Typed runtime settings in Laravel Go
Scope and alternative
Bring one database-backed, non-secret settings group into the app: for example, a display title and registration-enabled flag. This is not a generic settings framework, PHP reflection emulator, encrypted-secret migration, or Redis adapter. At the time of writing, the Package Port catalog listed no Go port; a catalog observation is not proof that no implementation exists. Check get-recipe's current port catalog before starting. This guidance has not been tested in your app.
Preserve these source behaviors deliberately
The baseline PHP package loads only declared properties from the group's repository, applies casts and decryption, uses declared defaults for missing properties, and rejects missing properties without defaults. A defaulted read does not prove a row was migrated; saving a class with unmigrated defaulted properties can fail. The database repository stores separately JSON-encoded payloads at unique (group, name) keys.
Locked properties are omitted from writes and their stored values are reloaded. That flag is a write-policy mechanism, not transaction isolation. Optional serialized caches and lazily loaded objects can outlive a single property read. None of those behaviors is supplied just by creating a Go struct.
Implementation steps
- Inventory every caller, property, repository override, cast, default, event, cache setting, encryption declaration, and non-HTTP writer for the chosen group. Reuse existing storage without rewriting other groups. Keep encrypted and custom-cast properties on the package-backed path until separately designed and verified. Do not attempt PHP object unserialization in Go.
- Give the selected group an app-owned typed snapshot and explicit load/update functions. Use the app's existing database/query layer with a context carrying its connection. Select a fixed group and fixed property names; never let client input choose SQL identifiers or settings groups. Return only the public subset required by the caller.
- Decode each payload exactly once.
encoding/jsoninto a map ofjson.RawMessagecan help check row/key presence before decoding individual typed fields. Missing, null, invalid JSON, andfalseare different cases; a zero-valued Go boolean is not evidence a required property exists. Check the driver returns raw JSON bytes/string rather than an already decoded object, and keep its representation consistent on writes. - Authenticate, authorize, and validate a bounded patch. For a read-modify-write group, use
db.Transaction(ctx, func(txctx context.Context) error { ... })fromgo.laravel.com/db. All queries inside must usetxctx; using the original context can bypass the transaction. Lock required existing rows in deterministic order on a supported production database and read the latestlockedflags there. Reject missing required rows rather than racing to initialize them. - Compare an expected snapshot or existing revision marker after locking so a stale administrator form cannot overwrite a newer edit. Apply only supplied allowed keys, preserving unrelated settings and locked values. Use a clear conflict/error policy rather than silently returning a struct containing an unsaved locked value. Every writer must share the locking protocol; SQLite needs a compatible strategy rather than an assumption that
FOR UPDATEworks. - Load immutable snapshots per request/job instead of using a mutable process-wide singleton. Defer caching until measured. If retaining cache, key by group and tenant where appropriate and update/invalidate after commit. Queue jobs must resolve fresh tenant/context state. Cache failure after commit is not a database rollback and must not trigger a blind duplicate write.
Security and migration boundaries
Runtime settings are not deployment secrets. PHP encryption payloads are not assumed compatible with Go, and caches/logs must not become a plaintext secret store. A cache prefix does not add tenant predicates to database queries. New schema, type conversions, removal of encryption, and destructive cleanup require an explicit rollout plan and approval, not automatic application of this recipe.
Trial checks
- Load a Unicode title,
false, and an empty string; distinguish each from missing/null/corrupt payloads. - Unauthorized callers cannot write. A locked field retains its persisted value even when a stale cache says it is unlocked.
- Two independent connections edit different fields and then conflicting snapshots. Assert exact final values and the declared stale-write conflict.
- Fail the second write: both values remain original from a fresh connection, exposing accidental use of the wrong context.
- Warm a cache, commit a write, and read from a second process/worker. Assert freshness, tenant isolation, and preservation of unrelated cache entries.
- Compare default-read, missing-row-save, and locked-field behavior with the source-backed caller. Document deliberate differences before removing that group's package dependency.
Run gofmt, go vet ./..., go test ./..., go test -race ./..., plus production-driver transaction/concurrency tests. The race detector alone does not test database lost updates. Record actual framework/Go versions and test output with the change.
Sources
- MIT reference: spatie/laravel-settings 3.10.3, especially
SettingsMapper.php,SettingsConfig.php,SettingsRepositories/DatabaseSettingsRepository.php,SettingsCache.php,SettingsEventSubscriber.php,tests/SettingsTest.php, anddatabase/migrations/create_settings_table.php.stub. RetainLICENSE.mdfor adapted source. - Laravel Go baseline, module
go.laravel.com; inspectdb/tx.goagainst the app's locked revision. - Port options: check
get-recipe's current port catalog (sourced from Package Port) before choosing an implementation.