danielemontecchi / laravel-scoped-settings
A Laravel package for global and model-scoped application settings.
v1.1.0
2025-04-17 15:54 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- nunomaduro/larastan: ^3.3
- orchestra/testbench: ^10.2
- pestphp/pest: ^3.8
- phpstan/phpstan: ^2.1
README
Laravel Scoped Settings is a lightweight Laravel package that provides a simple and elegant way to manage global and model-scoped application settings.
- โก Supports global settings and per-model scoped settings
- ๐ฏ API via Facade and
setting()
helper - โ Includes artisan commands and full test suite
- ๐ Optional per-scope caching built into
set()
andget()
๐ฆ Installation
You can install the package via Composer:
composer require danielemontecchi/laravel-scoped-settings
To customize default cache behavior, publish the config file:
php artisan vendor:publish --tag="laravel-scoped-settings-config"
To publish and run the migrations:
php artisan vendor:publish --tag="laravel-scoped-settings-migrations"
php artisan migrate
๐ Usage
// Global settings setting()->set('site.name', 'My App'); $siteName = setting()->get('site.name'); // Set with optional caching (global or scoped) setting()->set('site.name', 'My App', 3600); // Cache for 1 hour // If TTL is omitted, uses default from config (or disables cache) setting()->set('site.name', 'My App'); // No cache if config is null // Check if a setting exists (ignores fallback) if (setting()->has('site.name')) { // the value is explicitly set } // Scoped settings (e.g. per user) setting()->for($user)->set('dashboard.layout', 'compact'); $layout = setting()->for($user)->get('dashboard.layout');
You can also retrieve all flat or grouped settings:
setting()->all(); // ['site.name' => 'My App'] setting()->for($user)->group('dashboard'); // ['layout' => 'compact']
Clear a specific key:
setting()->forget('site.name');
๐ Artisan Commands
php artisan settings:list # Show all settings in CLI php artisan settings:clear # Clear all settings php artisan settings:export # Export settings to JSON php artisan settings:import # Import settings from JSON
๐งช Testing
./vendor/bin/pest
Coverage reports are generated via phpunit.xml
config and uploaded to Codecov.
๐ Documentation
Full documentation available at:
๐ https://danielemontecchi.github.io/laravel-scoped-settings
Includes:
- Installation & Configuration
- Usage Examples
- Scoping Strategies
- Artisan Commands
- Advanced Tips
๐ License
The MIT License (MIT). Please see License File for more information.