chapdel/model-settings-for-laravel
Attach flexible key/value settings to any Eloquent model in Laravel. Store, retrieve, and update per-model preferences with a simple API and database table, keeping configuration close to the data it belongs to. PHP 8+ compatible.
Installation
composer require chapdel/model-settings-for-laravel
Publish the config (if needed):
php artisan vendor:publish --provider="Chapdel\ModelSettings\ServiceProvider"
Define a Settings Model
Extend Chapdel\ModelSettings\Settings and register it in config/model-settings.php:
'models' => [
'App\Models\SiteSettings' => [
'key' => 'site',
'guard' => null, // or 'web', 'api', etc.
],
],
First Use Case: Fetching Settings
use App\Models\SiteSettings;
// Get all settings
$settings = SiteSettings::get();
// Get a single setting
$logo = SiteSettings::get('logo');
Caching for Performance
Enable caching in config/model-settings.php:
'cache' => true,
Clear cache manually when needed:
php artisan model-settings:clear-cache
Scoped Settings (Multi-Tenant)
Use the guard option to scope settings by user/tenant:
// In a controller/middleware
SiteSettings::setGuard('admin');
$adminSettings = SiteSettings::get('admin_panel');
Validation & Defaults
Define defaults in the model’s $casts or $attributes:
protected $casts = [
'logo' => 'string',
'maintenance_mode' => 'boolean',
];
// Fallback defaults
public static function defaults()
{
return [
'logo' => 'default-logo.png',
'maintenance_mode' => false,
];
}
Dynamic Updates Update settings via API or admin panel:
SiteSettings::set('maintenance_mode', true);
SiteSettings::save(); // Persist changes
Event Listeners Listen for setting changes (e.g., broadcast updates):
SiteSettings::set('theme', 'dark');
event(new SettingsUpdated(SiteSettings::class, 'theme'));
Cache Invalidation
php artisan model-settings:clear-cache
php artisan model-settings:clear-cache in a post-update hook (e.g., after SettingsUpdated event).Guard Mismatches
null if the guard isn’t set correctly.SiteSettings::setGuard('web');
$settings = SiteSettings::get();
Database Schema
settings table with key, value, and guard columns.php artisan vendor:publish --tag="model-settings-migrations"
php artisan migrate
Type Casting Issues
dd($settings->toArray()) to verify data types.Concurrency Conflicts
DB::transaction(function () {
SiteSettings::set('key', 'value');
SiteSettings::save();
});
Custom Storage Override the default storage (e.g., Redis) by binding a custom repository:
$this->app->bind(
\Chapdel\ModelSettings\Repositories\SettingsRepository::class,
\App\Repositories\CustomSettingsRepository::class
);
Encrypted Fields Use Laravel’s encryption for sensitive settings:
protected $casts = [
'api_key' => 'encrypted',
];
Soft Deletes Enable soft deletes in the model:
use Illuminate\Database\Eloquent\SoftDeletes;
class SiteSettings extends Settings {
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Observers Add logic before/after saves:
SiteSettings::observe(SettingsObserver::class);
Testing Mock settings in tests:
SiteSettings::shouldReceive('get')->andReturn(['test' => 'value']);
How can I help you explore Laravel packages today?