Installation:
composer require solomon-ochepa/laravel-settings
For Laravel <5.5, manually register the service provider and facade in config/app.php.
Publish Migration (if needed):
php artisan vendor:publish --provider="SolomonOchepa\Settings\SettingsServiceProvider" --tag="migrations"
php artisan migrate
First Use Case:
// Store a setting
Settings::set('app.name', 'My Awesome App');
// Retrieve a setting
$appName = Settings::get('app.name');
// Get all settings (cached)
$allSettings = Settings::all();
Settings facade for core operations.config/settings.php for cache driver).settings table with key, value, and group columns.Storing & Retrieving Settings:
// Set a single setting
Settings::set('theme.color', '#3498db');
// Get a single setting (with fallback)
$color = Settings::get('theme.color', '#000000');
// Set multiple settings at once
Settings::setMultiple([
'app.timezone' => 'America/New_York',
'mail.from.address' => 'contact@example.com',
]);
Grouped Settings:
// Store in a group (e.g., 'payment')
Settings::set('payment.gateway', 'stripe', 'payment');
// Retrieve all settings in a group
$paymentSettings = Settings::getGroup('payment');
Environment-Specific Settings:
// Set environment-specific (e.g., 'production')
Settings::set('api.key', 'prod_123', null, 'production');
// Retrieve environment-specific
$apiKey = Settings::get('api.key', null, 'production');
Configuration Integration:
// Override Laravel config with settings
config(['app.name' => Settings::get('app.name', config('app.name'))]);
boot():
public function boot()
{
$this->app['config']->set('app.name', Settings::get('app.name', config('app.name')));
}
public function handle($request, Closure $next)
{
if (Settings::get('maintenance.mode')) {
abort(503);
}
return $next($request);
}
return response()->json([
'app' => [
'name' => Settings::get('app.name'),
'version' => Settings::get('app.version'),
],
]);
Cache Invalidation:
php artisan cache:clear
Settings::refresh();
Serialization Issues:
Settings::set('user.permissions', ['admin', 'editor']); // Works
Settings::set('user.permissions', new stdClass()); // Fails (non-serializable)
Environment Conflicts:
Settings::get('api.key'); // Falls back to default env if not set
Migration Conflicts:
settings table exists, publishing the migration may fail. Skip or manually merge:
php artisan vendor:publish --tag="migrations" --force
dd(Settings::getCacheStore()); // Inspect cached data
Settings::disableCache();
// ... perform operations ...
Settings::enableCache();
Settings::has('key') to check existence before retrieval.Custom Cache Drivers:
config/settings.php:
'cache_driver' => 'redis',
SolomonOchepa\Settings\Contracts\CacheStore interface.Value Encryption:
SolomonOchepa\Settings\SettingsServiceProvider to encrypt sensitive values:
use Illuminate\Support\Facades\Crypt;
// In a custom provider
$value = Crypt::encrypt($value);
Settings::set($key, $value);
Event Listeners:
settings.saved or settings.deleted events to trigger side effects:
Settings::saved(function ($key, $value) {
Log::info("Setting $key updated to $value");
});
Validation Rules:
Settings::set('max.users', 100, null, null, function ($value) {
return is_numeric($value) && $value > 0;
});
How can I help you explore Laravel packages today?