- How do I replace hardcoded Laravel config values (e.g., `config('app.site_name')`) with strongly typed settings?
- Use the `make:setting` Artisan command to generate a settings class (e.g., `GeneralSettings`) with typed properties like `public string $site_name`. Inject the class into controllers/services via dependency injection (e.g., `public function __construct(GeneralSettings $settings)`), then replace direct config calls with `$settings->site_name`. The package handles the rest, including storage and caching.
- Which Laravel versions and PHP versions does `spatie/laravel-settings` support?
- The package supports Laravel 9.x, 10.x, and 11.x, with PHP 8.1+ required. Check the [GitHub repo](https://github.com/spatie/laravel-settings) for the latest compatibility matrix, as newer Laravel releases may require minor updates. Always test in a staging environment before upgrading.
- Can I use Redis or other storage backends instead of the default database repository?
- Yes. The package includes a Redis repository out of the box. Extend the `SettingsRepository` interface to create custom repositories (e.g., for DynamoDB or Memcached). Configure your preferred repository in the `settings.php` config file published via `vendor:publish`. For high-read workloads, Redis is a popular choice.
- How do I handle settings migrations when deploying to production?
- Run `php artisan make:settings-migration` to generate a migration for your settings class. Include it in your CI/CD pipeline alongside database migrations. Use Laravel’s `migrate:fresh` or `migrate:status` to verify migrations are applied. For rollbacks, ensure your settings migrations are idempotent or use a backup strategy for critical configurations.
- Is there a way to cache settings globally or per-request to improve performance?
- Enable caching by setting `SETTINGS_CACHE_ENABLED=true` in your `.env`. By default, settings are cached globally. For multi-tenant apps, override the `getCacheKey()` method in your settings class to include tenant-specific logic. Cache invalidation happens automatically after `save()` calls, but monitor TTL settings for stale data in distributed environments.
- How can I validate settings updates before saving them to the database/Redis?
- Use Laravel’s Form Request classes (e.g., `php artisan make:request UpdateGeneralSettingsRequest`). Define validation rules in the `rules()` method to match your settings class properties. Inject the request into your update method alongside the settings class, then call `$settings->save()` only after validation passes. This ensures type safety and consistency.
- What’s the best way to test settings in Laravel unit/feature tests?
- Mock the settings class in unit tests by binding a fake implementation to the container using `app()->bind(GeneralSettings::class, fn() => new FakeGeneralSettings)`. For feature tests, use `RefreshDatabase` or seed test data via migrations. Avoid global state pollution by resetting cached settings between tests with `$settings->resetCache()`.
- Can I restrict access to settings updates (e.g., admin-only) without reinventing the wheel?
- Leverage Laravel’s built-in auth middleware (e.g., `auth:sanctum` or `can:update-settings`) in your update routes/controllers. The package doesn’t include RBAC, but you can extend the `Settings` class or use policy classes to enforce granular permissions. For example, add `authorize('update', $settings)` before calling `save()`.
- Are there alternatives to `spatie/laravel-settings` for strongly typed configs in Laravel?
- Yes. Consider `beberlei/attributes` for runtime metadata (though less Laravel-specific), `laravel-ide-helper` for static analysis, or `spatie/laravel-data` for immutable DTOs. For database-backed configs, `laravel-config` or `beyondcode/laravel-settings` (YAML/JSON-based) are lighter but lack type safety. Evaluate based on your need for IDE support, storage flexibility, and caching.
- How do I audit or log changes to settings for compliance or debugging?
- Extend the `Settings` class to log changes via Laravel’s `Log` facade or a dedicated audit trail (e.g., `spatie/laravel-audit-logs`). Override the `save()` method to record timestamps, user IDs (from `Auth::user()`), and old/new values. For compliance, combine with Laravel’s `activitylog` package or a database trigger to track all modifications.