glorand/laravel-model-settings
Add per-model settings to Laravel Eloquent with an easy API to get/set/check/remove values. Choose storage via JSON field, separate settings table, or Redis. Supports defaults, validation, and persistence for user or entity preferences.
ModelSettingsSaved, ModelSettingsRestored, and ModelSettingsDeleted events, enabling integration with Laravel’s event system (e.g., broadcasting, queues, or notifications).ModelSettings::cache()) leverages Laravel’s cache drivers (Redis, Memcached, etc.), improving performance for read-heavy workloads.HasModelSettings trait and define a settings table. Compatible with Laravel’s conventions (e.g., migrations, service providers).settings table per model (or a shared table with a model_type discriminator). May introduce schema complexity if not planned early.
with('settings')) can inflate query counts. Use caching aggressively for frequently accessed settings.lockForUpdate() or application-level retries.config() or environment variables.model_type/model_id) vs. per-model tables? Trade-offs: query complexity vs. normalization.User::find(auth()->id())->settings).User, Product, Tenant).User preferences).settings table:
Schema::create('user_settings', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->json('settings')->nullable();
$table->timestamps();
});
use Glorand\LaravelModelSettings\Traits\HasModelSettings;
class User extends Authenticatable {
use HasModelSettings;
}
user->settings->theme).validateSettings().composer.json:
"replace": {
"laravel/framework": "8.*"
}
json and pdo extensions (standard in Laravel).settings table(s) via migrations.HasModelSettings trait on target models.SettingsController).UserSettingsTest).settings tables are included in database backups (critical for user preferences or tenant configs).DB::enableQueryLog();
$user->settings; // Check lastQuery() for performance insights.
ModelSettingsException and log errors.try {
$user->settings->update(['theme' => 'dark']);
} catch (\Exception $e) {
Log::error("Failed to update settings: {$e->getMessage()}");
// Fallback to default settings.
}
model_id in the settings table for faster lookups.settings tables (e.g., by model_type).User::with('settings')->get();
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database downtime | Settings inaccessible | Use cache as a fallback; implement stale-while-revalidate. |
| Cache failure | Stale settings served | Shorten cache TTL; log cache misses. |
| Concurrent updates | Lost updates (race conditions) | Implement optimistic locking or retries. |
| JSON corruption | Invalid settings data |
How can I help you explore Laravel packages today?