Laravel PHP · Inline✓ Human-reviewed

App-owned runtime settings in Laravel PHP

Inline one non-secret settings group using existing JSON rows, typed snapshots, authorized patches, stale-edit protection, and targeted post-commit cache invalidation.

Approved 2026-12-04T13:20:43.479109Z · Submission 71

App-owned runtime settings in Laravel PHP

Replace a group, not a settings framework

Inline one non-secret, database-backed settings group using the app's existing Eloquent/query layer and a small typed value object. Keep its existing persisted values. Reflection discovery, settings migration runners, Redis storage, custom casts, and secret encryption are out of scope. If those are integral to the chosen group, retain the package or expand the reviewed plan rather than silently dropping behavior. Verify it in your app with the acceptance checks below.

Source behavior to preserve

The reference mapper loads declared properties, applies casts/decryption, then fills declared defaults and rejects missing required values. Defaulted reads are not migrations: saving can fail when default-backed rows were never created. The database repository stores one JSON payload per unique (group, name). The locked flag prevents a property from being saved and causes its persisted value to be reloaded; it is not a database mutex.

The package emits loading/saving events and optionally caches serialized settings objects. Inventory listeners and repository/cache overrides before replacing them. A PHP object resolved once in a worker will not magically refresh when another process changes the database.

Application-owned design

  1. Inventory settings classes, property names/types, defaults, casts, encrypted()/encryption attributes, events, calls to save, locking, CLI writers, and tenant-specific prefixes. Select only the non-secret group whose behavior you can describe. Do not move runtime configuration into .env merely to avoid a persistence layer.
  2. Read existing rows with fixed group/name predicates. Decode payloads using a single explicit representation. If retaining a raw payload column, json_decode($row->payload, true, 512, JSON_THROW_ON_ERROR) and one matching encode are appropriate; if an Eloquent JSON cast already decodes it, do not decode/encode again. Validate decoded types and missing/null/default cases before constructing the typed snapshot.
  3. Use a dedicated authorized update action, with an allowlist of permitted fields rather than mass-assigning request input. Validate string lengths/boolean values and reject unknown keys. Read-only public callers receive only the explicitly public fields, never the whole group.
  4. In DB::transaction, lock required existing rows in a deterministic order with lockForUpdate() on the production connection, then read their current locked flags and values. A cached object cannot authorize a write. Missing rows need an explicit initialization plan; a lock on an absent row is not sufficient. For SQLite, test a compatible serialization strategy rather than assuming row-level locks.
  5. Apply only supplied unlocked fields. Compare the administrator's expected snapshot after locking, or use an existing revision mechanism, to reject stale edits. All writers must use the same protocol. Preserve locked values in the returned snapshot; choose and document whether attempted edits are rejected or ignored. A whole-object save based on a stale read can lose another writer's change.
  6. Start without cross-request caching unless it is needed. Pass a per-operation immutable value object. If retaining cache/events, publish freshness changes only after commit and scope invalidation to the group's actual key. A global cache flush is not acceptable. Reset any request-scoped binding correctly in long-lived workers. Failed post-commit cache work does not imply the database rolled back.
  7. Preserve encrypted/custom-cast groups on their old path. Retain application encryption keys and compatible decryptors until an independently reviewed migration proves old data remains usable. Do not decrypt secrets into logs, Git defaults, broad JSON responses, or unencrypted caches.

Before removing the package

Record output and exceptions for valid values, missing rows, defaulted reads, nulls, invalid JSON, and locked writes on the package-backed caller. The app-owned implementation may deliberately reject writes that were previously ignored, but that changes the API contract and needs explicit acceptance. Existing settings migrations must still run in deployment order until the application owns their replacement; do not delete them during the first group extraction.

Acceptance checks

  • Load existing Unicode titles and boolean false accurately; malformed or absent payloads cannot silently become empty/false defaults.
  • Deny unauthorized writes, reject unknown names, and keep locked values unchanged even with stale cached settings.
  • Exercise simultaneous independent-connection edits to different and identical fields; verify unrelated changes survive and stale snapshots conflict.
  • Inject failure between two writes and assert a fresh connection sees neither change. No success event or fresh-cache snapshot may escape the rollback.
  • A second HTTP request and a long-lived worker observe the committed group, with tenant isolation and unrelated cache entries preserved.
  • Compare source-backed default/missing-row behavior and any subscribed events. Only then migrate remaining callers and inspect composer why spatie/laravel-settings before removing the package.

Run the application's settings feature tests and a production-driver concurrency test. Record actual PHP/Laravel versions, storage representation, changed behavior, and decisive commands/results with the change.

Sources

  • MIT reference: spatie/laravel-settings 3.10.3. Read Settings.php, SettingsMapper.php, SettingsConfig.php, SettingsRepositories/DatabaseSettingsRepository.php, SettingsCache.php, SettingsEventSubscriber.php, Support/Crypto.php, the migration stub, and tests/SettingsTest.php. Retain the upstream LICENSE.md for adapted source.
  • Laravel PHP framework baseline. Match the real application's lockfile and supported transaction/after-commit APIs before editing.