A powerful and flexible Laravel package for managing application settings with database storage, caching support, and Artisan commands.
- 📦 Database Storage - Store settings in database with serialization support
- 🚀 Caching - Built-in caching support for improved performance
- 🎨 Multiple Data Types - Support for strings, integers, booleans, arrays, and objects
- 🛠️ Artisan Commands - Manage settings via command line
- 🧪 Fully Tested - Comprehensive test coverage
- 🎯 Simple API - Easy-to-use static methods and helper functions
- 🔧 Laravel Integration - Seamless integration with Laravel's service container
- Installation
- Configuration
- Basic Usage
- API Reference
- Artisan Commands
- Advanced Usage
- Testing
- Troubleshooting
- Contributing
- License
Install the package via Composer:
composer require firdavs9512/laravel-settingAfter installation, publish the configuration file and migration:
php artisan vendor:publish --provider="Firdavs9512\LaravelSetting\LaravelSettingServiceProvider"Run the migration to create the settings table:
php artisan migrateThe configuration file is located at config/laravel-setting.php:
return [
// Cache configuration
'cache' => [
'enabled' => true, // Enable/disable caching
'store' => 'file', // Cache store to use
'expiration' => 3600, // Cache expiration in seconds
],
// Database table prefix
'table_prefix' => 'laravel', // Results in 'laravel_settings' table
];- enabled: Set to
trueto enable caching for better performance - store: Specify the cache store (file, redis, memcached, etc.)
- expiration: Cache lifetime in seconds (default: 1 hour)
use Firdavs9512\LaravelSetting\Setting;
// Set simple values
Setting::set('app.name', 'My Application');
Setting::set('app.version', '1.0.0');
Setting::set('app.debug', true);
// Set arrays
Setting::set('app.features', ['feature1', 'feature2', 'feature3']);
// Set objects
Setting::set('app.config', (object)['key' => 'value']);// Get with default value
$name = Setting::get('app.name', 'Default App');
// Get without default (returns null if not found)
$version = Setting::get('app.version');
// Get array
$features = Setting::get('app.features', []);// Get value
$name = setting('app.name', 'Default App');
// Set value
setting()->set('app.name', 'New Name');
// Check existence
$exists = setting()->has('app.name');
// Delete setting
setting()->delete('app.name');Store a setting value in the database.
Setting::set('site.title', 'My Website');
Setting::set('site.options', ['option1', 'option2']);Retrieve a setting value by key.
$title = Setting::get('site.title', 'Default Title');Check if a setting exists.
if (Setting::has('site.title')) {
// Setting exists
}Delete a setting by key.
Setting::delete('site.title');Alias for delete() method.
Setting::forget('site.title');Get all settings as an associative array.
$allSettings = Setting::all();
// Returns: ['key1' => 'value1', 'key2' => 'value2', ...]Delete all settings from the database.
Setting::flush(); // Removes all settingsGet a setting value or store the result of a callback if it doesn't exist.
$value = Setting::remember('expensive.calculation', function () {
return performExpensiveCalculation();
});The package provides several Artisan commands for managing settings via CLI:
php artisan setting:get {key}Example:
php artisan setting:get app.name
# Output: Setting 'app.name': My Applicationphp artisan setting:set {key} {value} [--type=auto]Available types: auto, string, int, bool, json, array
Examples:
# Auto-detect type
php artisan setting:set app.name "My App"
# Explicit integer
php artisan setting:set max.users 100 --type=int
# Boolean value
php artisan setting:set app.debug true --type=bool
# JSON/Array value
php artisan setting:set app.features '["feature1","feature2"]' --type=jsonphp artisan setting:delete {key} [--force]Examples:
# With confirmation
php artisan setting:delete app.old_feature
# Without confirmation
php artisan setting:delete app.old_feature --forcephp artisan setting:list [--filter=] [--json]Examples:
# List all settings in table format
php artisan setting:list
# Filter by key pattern
php artisan setting:list --filter=app
# Output as JSON
php artisan setting:list --jsonphp artisan setting:flush [--force]Examples:
# With confirmation
php artisan setting:flush
# Without confirmation (careful!)
php artisan setting:flush --force// Store user preferences
Setting::set('user.preferences', [
'theme' => 'dark',
'language' => 'en',
'notifications' => [
'email' => true,
'push' => false,
]
]);
// Retrieve nested data
$preferences = Setting::get('user.preferences');
$theme = $preferences['theme'] ?? 'light';You can integrate settings with Laravel's config system:
// In a service provider's boot method
public function boot()
{
config(['app.name' => Setting::get('app.name', config('app.name'))]);
}For frequently accessed settings:
// Use remember method for computed values
$value = Setting::remember('computed.value', function () {
// Expensive operation
return computeValue();
});To manually clear cache:
// Clear specific setting cache
Cache::forget('laravel-setting-app.name');
// Or disable cache temporarily
config(['laravel-setting.cache.enabled' => false]);Organize settings using dot notation:
// Email settings
Setting::set('mail.driver', 'smtp');
Setting::set('mail.host', 'smtp.example.com');
Setting::set('mail.port', 587);
Setting::set('mail.encryption', 'tls');
// Payment settings
Setting::set('payment.gateway', 'stripe');
Setting::set('payment.currency', 'USD');
Setting::set('payment.test_mode', true);Run the test suite using PHPUnit:
# Run all tests
vendor/bin/phpunit
# Run with coverage
vendor/bin/phpunit --coverage-html coverage
# Run specific test suite
vendor/bin/phpunit --testsuite=Unit
vendor/bin/phpunit --testsuite=FeatureWhen testing your application with this package:
use Firdavs9512\LaravelSetting\Setting;
class YourTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// Clear settings before each test
Setting::flush();
}
public function test_your_feature()
{
// Set up test settings
Setting::set('test.key', 'test value');
// Your test logic
$this->assertEquals('test value', Setting::get('test.key'));
}
}- Check database connection
- Ensure migrations have been run
- Verify table name matches configuration
- Clear application cache:
php artisan cache:clear - Check cache configuration in
config/laravel-setting.php - Verify cache driver is properly configured
- Check if table already exists
- Verify database user has CREATE TABLE permissions
- Check table prefix configuration
- Enable caching for production environments
- Use appropriate cache drivers (Redis/Memcached for high-traffic sites)
- Group related settings using dot notation
- Avoid storing large objects directly; consider using file storage with path references
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Added
has(),delete(),all(),flush(),forget(), andremember()methods - Added comprehensive Artisan commands
- Improved test coverage
- Enhanced documentation
- Initial stable release
- Basic get/set functionality
- Cache support
- Database storage
This package is open-source software licensed under the MIT license.
- Author: Firdavs (firdavs20fe@gmail.com)
- Contributors: All Contributors
For issues, questions, or suggestions, please open an issue on GitHub.