larapacks/setting
Laravel package for storing and retrieving application settings with a simple API. Manage key/value configuration in your database, access values via helpers or facades, and keep defaults in code while allowing runtime overrides for per-app customization.
Installation:
composer require larapacks/setting
Publish the migration and config:
php artisan vendor:publish --provider="Larapacks\Setting\SettingServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Larapacks\Setting\SettingServiceProvider" --tag="config"
Run the migration:
php artisan migrate
First Use Case: Define a setting via Tinker or a seed:
use Larapacks\Setting\Facades\Setting;
Setting::set('app.max_upload_size', '10MB');
Retrieve it in code:
$maxUploadSize = Setting::get('app.max_upload_size');
Where to Look First:
Larapacks\Setting\Facades\Setting (e.g., Setting::get(), Setting::set()).config/setting.php (adjust cache drivers, storage, etc.).database/migrations/[timestamp]_create_settings_table.php (customize columns if needed).Feature Flags:
if (Setting::get('features.new_ui')) {
return view('new-ui');
}
Dynamic Configuration: Replace hardcoded values with settings:
$apiRateLimit = Setting::get('api.rate_limit', 100); // Default fallback
Environment-Specific Overrides: Useful for staging/production tweaks without redeploying:
Setting::set('app.debug_mode', env('APP_DEBUG') ? 'true' : 'false');
Caching for Performance:
Enable caching in config/setting.php:
'cache' => [
'driver' => 'file', // or 'redis', 'database'
'prefix' => 'setting_',
],
Clear cache when settings change:
Setting::clearCache();
$validated = Setting::set('app.timeout', request()->input('timeout'), [
'numeric',
'min:1',
'max:300',
]);
SettingUpdated event:
Setting::updated(function ($key, $oldValue, $newValue) {
Log::info("Setting '$key' changed from '$oldValue' to '$newValue'");
});
Setting::set('app.default_currency', 'USD');
Setting::set('app.support_email', 'support@example.com');
Cache Invalidation:
php artisan setting:clear-cache).Setting::clearCache() or configure auto-clearing in config/setting.php.Type Mismatches:
$isEnabled = (bool) Setting::get('features.dark_mode');
Overwriting Defaults:
config/setting.php.return [
'storage' => [
'driver' => 'database',
],
// Override only what's needed
];
Migration Conflicts:
settings table schema after initial migration.Schema::table().database or cache).
Setting::getStorage()->getDriver(); // Inspect current driver
$value = Setting::get('app.unknown_setting', null);
if (is_null($value)) {
Log::warning("Missing setting: app.unknown_setting");
}
config/setting.php to isolate issues:
'cache' => [
'driver' => 'null',
],
Custom Storage:
Implement Larapacks\Setting\Contracts\SettingStorage for alternative backends (e.g., S3, external APIs):
class S3SettingStorage implements SettingStorage {
// ...
}
Register in config/setting.php:
'storage' => [
'driver' => 's3',
'class' => \App\Services\S3SettingStorage::class,
],
Scoped Settings: Use middleware to set request-specific scopes:
Setting::scope('tenant', $tenantId);
$value = Setting::get('app.theme'); // Resolves to tenant-scoped value
Encrypted Settings: Combine with Laravel’s encryption for sensitive values:
$encryptedValue = Setting::get('app.api_key');
$decryptedValue = decrypt($encryptedValue);
How can I help you explore Laravel packages today?