elegantly / laravel-settings
Settings for your Laravel App
Fund package maintenance!
ElegantEngineeringTech
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0||^10.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
A simple and flexible way to manage global or model-specific settings in your Laravel app.
Features
- Define global settings
- Store settings in the database via Eloquent
- Cache settings for performance
- Attach settings to users or any other model
- Support for typed namespaced settings classes
Table of Contents
Installation
Install the package via Composer:
composer require elegantly/laravel-settings
Publish the migration and run it:
php artisan vendor:publish --tag="settings-migrations"
php artisan migrate
Optionally, publish the configuration file:
php artisan vendor:publish --tag="settings-config"
Configuration
Published config file (config/settings.php
):
use Elegantly\Settings\Models\Setting; return [ /* * The Eloquent model used to store and retrieve settings */ 'model' => Setting::class, /* * Cache configuration for global settings */ 'cache' => [ 'enabled' => true, 'key' => 'settings', 'ttl' => 60 * 60 * 24, // 1 day ], ];
Usage
Basic Usage (Facade)
use Elegantly\Settings\Facades\Settings; // Set a value Settings::set( namespace: 'home', name: 'color', value: 'white' ); // Get a value $setting = Settings::get( namespace: 'home', name: 'color' ); $setting->value; // white
Dependency Injection
namespace App\Http\Controllers; use Elegantly\Settings\Settings; class UserController extends Controller { public function index(Settings $settings) { $settings->set( namespace: 'home', name: 'color', value: 'white' ); $setting = $settings->get( namespace: 'home', name: 'color' ); $setting->value; // white } }
Typed Namespaced Settings
For better DX, define custom typed settings classes:
Usage
namespace App\Http\Controllers; use App\Settings\HomeSettings; class UserController extends Controller { public function index(HomeSettings $settings) { $settings->color; // white $settings->color = 'black'; $settings->save(); } }
Defining a Typed Settings Class
namespace App\Settings; use Elegantly\Settings\NamespacedSettings; class HomeSettings extends NamespacedSettings { public ?string $color = null; /** @var int[] */ public array $articles = []; public static function getNamespace(): string { return 'home'; } }
Testing
Run the test suite:
composer test
Changelog
See CHANGELOG for details on recent changes.
Contributing
See CONTRIBUTING for contribution guidelines.
Security Vulnerabilities
Please review our security policy for reporting vulnerabilities.
Credits
License
This package is open-sourced under the MIT license.
Would you like a README badge for PHPStan or Laravel Pint added as well?