codersandip/laravel-dynamic-settings
Composer 安装命令:
composer require codersandip/laravel-dynamic-settings
包简介
A professional Laravel package to store and manage application settings dynamically using the database with caching, multi-tenant support, and an admin panel UI.
README 文档
README
A professional Laravel package to store and manage application settings dynamically using the database instead of config files. Supports automatic caching, multi-tenant scoping, per-user overrides, and a beautiful admin panel UI.
Features
- 📦 Database-driven settings — store any key/value with type casting
- ⚡ Automatic caching — configurable driver and TTL, cache invalidated on update
- 🏢 Multi-tenant support — tenant-scoped settings with global fallback
- 👤 Per-user overrides — priority: user → tenant → global
- 🎨 Admin Panel UI — dark-mode Bootstrap 5 CRUD interface with search, filter, toggles
- 🔄 Inline editing — AJAX quick-update widget on the edit page
- 🛡️ Type support —
string,integer,float,boolean,json - 🖥️ Artisan commands —
settings:cache,settings:clear,settings:seed - ✅ Fully tested — PHPUnit test suite included
Requirements
- PHP 8.1+
- Laravel 10.x / 11.x / 12.x
Installation
1. Install via Composer
composer require codersandip/laravel-dynamic-settings
Laravel auto-discovers the service provider. No manual registration needed.
2. Publish assets
Publish everything at once:
php artisan vendor:publish --tag=settings-all
Or selectively:
# Config only php artisan vendor:publish --tag=settings-config # Migrations only php artisan vendor:publish --tag=settings-migrations # Views only php artisan vendor:publish --tag=settings-views # Routes only php artisan vendor:publish --tag=settings-routes
3. Run migrations
php artisan migrate
4. (Optional) Seed default settings
php artisan settings:seed
Configuration
After publishing, edit config/settings.php:
return [ // Cache driver (file, redis, memcached, array) 'cache_driver' => env('SETTINGS_CACHE_DRIVER', 'file'), // Cache TTL in seconds (3600 = 1 hour) 'cache_ttl' => env('SETTINGS_CACHE_TTL', 3600), // Cache key prefix 'cache_prefix' => env('SETTINGS_CACHE_PREFIX', 'dyn_setting'), // Admin panel URL prefix 'route_prefix' => env('SETTINGS_ROUTE_PREFIX', 'admin/settings'), // Middleware applied to admin routes 'route_middleware' => ['web', 'auth'], // Available groups in the dropdown 'groups' => ['general', 'email', 'payment', 'security'], ];
You can also set these in your .env file:
SETTINGS_CACHE_DRIVER=redis SETTINGS_CACHE_TTL=7200 SETTINGS_ROUTE_PREFIX=dashboard/settings
Usage
Helper Function
// Get a setting setting('site_name'); // → 'My Application' // Get with a default value setting('site_name', 'Default App'); // → 'Default App' if not set // Get with tenant context (falls back to global if not found) setting('currency', 'USD', $tenantId); // Get with tenant + user context setting('theme', 'light', $tenantId, $userId);
Set a Setting
// Via helper (chained) setting()->set('site_name', 'My App'); // With options setting()->set('page_size', 20, [ 'type' => 'integer', 'group' => 'general', 'description' => 'Results per page', ]);
Facade
use Codersandip\DynamicSettings\Facades\Settings; Settings::get('site_name'); Settings::get('site_name', 'Default'); Settings::set('site_name', 'New Name'); Settings::delete('site_name'); Settings::has('site_name');
Service (Dependency Injection)
use Codersandip\DynamicSettings\Services\SettingsService; class MyController extends Controller { public function __construct(private SettingsService $settings) {} public function index() { $siteName = $this->settings->get('site_name', 'My App'); // ... } }
Data Types
| Type | PHP Type | Storage | Example |
|---|---|---|---|
string |
string |
As-is | "Hello World" |
integer |
int |
Numeric string | 42 |
float |
float |
Numeric string | 3.14 |
boolean |
bool |
"1" / "0" |
true / false |
json |
array |
JSON string | ["stripe", "paypal"] |
setting()->set('enabled', true, ['type' => 'boolean']); setting()->set('count', 100, ['type' => 'integer']); setting()->set('gateways', ['stripe', 'pp'], ['type' => 'json']); setting('enabled'); // → true (bool) setting('count'); // → 100 (int) setting('gateways'); // → ['stripe', 'pp'] (array)
Multi-Tenant Support
Settings resolve with this priority: user → tenant → global
// Store global setting setting()->set('currency', 'USD'); // Store tenant-specific override setting()->set('currency', 'GBP', ['tenant_id' => 5]); // Store user-specific override (within a tenant) setting()->set('currency', 'EUR', ['tenant_id' => 5, 'user_id' => 12]); // Retrieval setting('currency'); // → 'USD' (global) setting('currency', null, 5); // → 'GBP' (tenant 5 override) setting('currency', null, 5, 12);// → 'EUR' (user 12 override) setting('currency', null, 5, 99);// → 'GBP' (user 99 falls back to tenant) setting('currency', null, 9); // → 'USD' (tenant 9 falls back to global)
Fluent API
use Codersandip\DynamicSettings\Facades\Settings; // Set tenant context once $tenantSettings = Settings::forTenant(5); $tenantSettings->get('currency'); // uses tenant 5 $tenantSettings->get('locale'); // Chain tenant + user Settings::forTenant(5)->forUser(12)->get('theme');
Artisan Commands
| Command | Description |
|---|---|
settings:cache |
Pre-warm cache with all settings from DB |
settings:clear |
Flush the settings cache |
settings:seed |
Seed a set of default settings |
php artisan settings:cache
php artisan settings:clear
php artisan settings:seed
php artisan settings:seed --force # overwrite existing settings
Admin Panel
Access the admin panel at /admin/settings (configurable via route_prefix).
The panel includes:
- 📋 Index — list all settings with search, group/type filter, boolean toggles
- ➕ Create — add a new setting with type, group, scope options
- ✏️ Edit — update a setting, with inline AJAX quick-update widget
- 🗑️ Delete — remove a setting (with confirmation)
Protecting the Admin Panel
Set the middleware in config/settings.php:
'route_middleware' => ['web', 'auth', 'can:manage-settings'],
Or disable the built-in routes and define your own:
// config/settings.php 'disable_routes' => true,
Then publish routes:
php artisan vendor:publish --tag=settings-routes
And register the file in bootstrap/app.php or routes/web.php.
Database Schema
settings
├── id (bigint, auto-increment)
├── key (varchar 191, indexed)
├── value (longtext, nullable)
├── type (varchar 20 — string|integer|float|boolean|json)
├── group (varchar 50, indexed, default: general)
├── tenant_id (bigint unsigned, nullable, indexed)
├── user_id (bigint unsigned, nullable, indexed)
├── description (varchar, nullable)
├── is_public (boolean, default: false)
├── created_at
└── updated_at
UNIQUE (key, tenant_id, user_id)
Testing
composer test
Changelog
See CHANGELOG.md.
License
The MIT License (MIT). See LICENSE for details.
Author
Sandip — contact@codersandip.com
GitHub: github.com/codersandip
codersandip/laravel-dynamic-settings 适用场景与选型建议
codersandip/laravel-dynamic-settings 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「laravel」 「Settings」 「admin-panel」 「multi-tenant」 「dynamic-settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codersandip/laravel-dynamic-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codersandip/laravel-dynamic-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codersandip/laravel-dynamic-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
Utils to load, parse and work with configuration on Mezzio projects
Extends basic Wordpress features.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-09