chapdel/settings
Add per-model settings to your Laravel app. Store settings in a JSON column, a dedicated table, or Redis via simple traits. Includes defaults plus helpers to get, set, check, remove, and persist settings on any Eloquent model.
Installation
composer require chapdel/settings
Publish the migration and config:
php artisan vendor:publish --provider="Chapdel\Settings\SettingsServiceProvider"
php artisan migrate
Define a Settings Model
Extend Chapdel\Settings\Settings and register it in config/settings.php:
'models' => [
'app' => \App\Models\AppSettings::class,
],
First Use Case Define a setting in your model:
use Chapdel\Settings\Settings;
class AppSettings extends Settings
{
protected $casts = [
'theme_color' => 'string',
'max_upload_size' => 'integer',
];
}
Access/update settings:
$settings = AppSettings::firstOrCreate(['key' => 'app']);
$settings->theme_color = '#3498db';
$settings->save();
Global vs. Scoped Settings
Use the key field to differentiate between settings scopes (e.g., app, user:123):
// Global app settings
$appSettings = AppSettings::where('key', 'app')->first();
// User-specific settings
$userSettings = AppSettings::where('key', 'user:'.$user->id)->first();
Configuration Caching
Cache settings for performance (e.g., in AppServiceProvider):
public function boot()
{
Cache::remember('app_settings', now()->addHours(1), function () {
return AppSettings::where('key', 'app')->first();
});
}
Validation & Defaults
Override getDefaultValues() in your model:
protected function getDefaultValues()
{
return [
'theme_color' => '#ffffff',
'max_upload_size' => 5, // MB
];
}
Dynamic Accessors Use accessors for computed settings:
public function getUploadSizeLimitAttribute()
{
return $this->max_upload_size * 1024 * 1024; // Convert MB to bytes
}
$this->app->singleton('settings.app', function () {
return AppSettings::where('key', 'app')->first();
});
public function handle($request, Closure $next)
{
$request->merge(['user_settings' => UserSettings::forUser($request->user())]);
return $next($request);
}
toArray() or toJson() with custom formatting:
return response()->json($settings->only(['theme_color', 'upload_size_limit']));
Key Collisions
Ensure key values are unique. Use namespacing (e.g., app:theme, user:123:notifications) to avoid conflicts.
Mass Assignment Risks
Explicitly define $fillable to prevent unintended setting overrides:
protected $fillable = ['theme_color', 'max_upload_size'];
Caching Invalidation Clear cache manually after updates:
Cache::forget('app_settings');
Model Events
Override saved() or deleted() to trigger side effects (e.g., cache invalidation):
protected static function boot()
{
parent::boot();
static::saved(function ($model) {
Cache::forget('app_settings');
});
}
DB::enableQueryLog();
$settings = AppSettings::where('key', 'app')->first();
dd(DB::getQueryLog());
firstOrCreate() with defaults to avoid null values:
$settings = AppSettings::firstOrCreate(['key' => 'app'], [
'theme_color' => '#ffffff',
]);
Custom Storage
Override getTable() or use traits to switch databases (e.g., Redis for caching):
protected $connection = 'redis';
Encryption Encrypt sensitive settings using Laravel’s encryption:
protected $casts = [
'api_key' => 'encrypted',
];
Events Dispatch events for settings changes:
protected static function boot()
{
static::saved(function ($model) {
event(new SettingsUpdated($model));
});
}
Policy Integration Restrict access to settings via policies:
class AppSettingsPolicy
{
public function update(User $user, AppSettings $settings)
{
return $user->isAdmin();
}
}
How can I help you explore Laravel packages today?