lukasss93/laravel-model-settings
Add per-model settings to Eloquent with defaults, validation rules, and optional config publishing. Store and retrieve settings directly on your models, initialize settings on creation, and keep your app flexible with PHP 8+ and Laravel 8+ support.
Strengths:
settingsRules()) reduce boilerplate and enforce consistency, critical for compliance-heavy or data-sensitive applications (e.g., SaaS platforms).defaultSettings() and initSettings simplify onboarding for new models, reducing manual setup in migrations or seeders.Weaknesses:
settings->language = 'en'). Requires application-level parsing or full-table scans for complex queries.Laravel Ecosystem Fit:
use HasSettingsField/Table/Redis) with zero configuration for basic use cases. Ideal for greenfield projects or incremental adoption.model-settings:model-settings-field/table automate schema setup, reducing manual migration errors.Migration Path:
defaultSettings method conversion) may require refactoring in legacy codebases..env or config/) by scoping settings to specific models (e.g., User::settings()->get('theme') vs. config('app.theme')).Performance:
DB::select("SELECT AVG(LENGTH(settings)) FROM users").model_id, setting_key composite index).redis-cli --latency before production.Data Integrity:
set()/apply(), not direct DB updates (e.g., via raw SQL). Use model observers or gates to enforce consistency.apply()/setMultiple. For critical settings (e.g., payment thresholds), implement optimistic locking ($model->settings()->set('threshold', $newValue, ['lock_for' => 5])).Testing:
test('nested_settings_preserve_structure')->assertEquals(
['a' => ['b' => 1]],
$model->settings()->set('a.b', 1)->get()
);
model-settings:model-settings-field (e.g., php artisan migrate:rollback).Storage Tradeoffs:
Product model with 1M rows, test:
ab -n 10000 -c 100 "GET /api/products/1/settings" # Field vs. Table latency
Schema Evolution:
user.theme to user.preferences.theme)? Options:
settings_migration_version field to track updates.settings()->get('user.theme', $settings->get('user.preferences.theme')).Access Control:
User settings) or global? If global, consider a Settings model with setting_key/setting_value columns.Observability:
set() in a logger (e.g., event(new SettingUpdated($model, $key, $oldValue, $newValue))).updated_at to the settings table/field.Team Adoption:
user->settings()->set('key', 'value') or a fluent builder (e.g., Settings::for($user)->set('key', 'value'))?Settings facade wrapper.Laravel Core:
ModelSettingsServiceProvider automatically, integrating with Laravel’s service container.Database:
json type or serialize manually.model_settings table with model_type, model_id, setting_key, setting_value columns. Customize via php artisan vendor:publish --tag="model-settings-config".Redis::hash for O(1) access. Configure in .env:
MODEL_SETTINGS_REDIS_CONNECTION=cache
MODEL_SETTINGS_REDIS_KEY_PREFIX=settings_
Caching:
Cache::remember) for high-read workloads. Example:
public function getSettings()
{
return Cache::remember(
"settings_{$this->id}",
now()->addHours(1),
fn() => $this->settings()->get()
);
}
Assessment Phase:
.env, config/, or custom tables) to identify candidates for migration.config('app.feature_flags') with App\Settings::get('feature_flags').Pilot Model:
NewsletterSubscription) to test:
settingsRules(['max_subscribers' => 'integer|max:1000'])).HasSettingsField for simplicity, switch to HasSettingsTable if settings exceed 1KB.Incremental Rollout:
Product::settings()->get('price_currency')).// app/Http/Middleware/DeprecateLegacyConfigs.php
public function handle($request, Closure $next)
{
if (config('app.legacy_configs_enabled')) {
Log::warning('Legacy config accessed', ['config' => config('app.theme')]);
}
return $next($request);
}
Schema Changes:
HasSettingsField:
php artisan model-settings:model-settings-field User
php artisan migrate
HasSettingsTable:
php artisan model-settings:model-settings-table User
php artisan migrate
Illuminate\Database\Eloquent\Model.json and pdo extensions. For Redis, install predis/predis.composer why lukasss93/laravel-model-settings to check dependencies.HasSettingsHow can I help you explore Laravel packages today?