hatchyu/laravel-settings
Composer 安装命令:
composer require hatchyu/laravel-settings
包简介
Enterprise-ready hierarchical multi-scope settings for Laravel with inheritance support, caching, and automatic value casting.
README 文档
README
A powerful, enterprise-ready Laravel package for managing application-level and user-level settings with hierarchical scope inheritance, caching, and automatic value casting.
Features
- Multi-Scope Support: Global, User, Team, Company, or any custom scope.
- Hierarchical Inheritance: Define fallback chains (e.g., User -> Team -> Global).
- Dot Notation: Access nested settings easily (
settings()->get('ui.theme.color')). - Automatic Casting: Supports integer, float, boolean, array, object, json, and string.
- Cache-Friendly: Built-in caching layer with automatic invalidation.
- Model Integration: Simple trait to add settings capability to any Eloquent model.
Installation
You can install the package via composer:
composer require hatchyu/laravel-settings
Run Migrations
The package requires two tables: setting_definitions and setting_values.
php artisan migrate
Configuration
The package automatically registers its Service Provider. If you need to manually register it, add this to config/app.php:
'providers' => [ Hatchyu\Settings\SettingsServiceProvider::class, ],
Basic Usage
Global Settings
Use the settings() helper or the Settings facade:
use Hatchyu\Settings\Facades\Settings; // Set a global setting settings()->set('app.name', 'My Super App'); // Get a global setting $name = settings()->get('app.name'); // 'My Super App' // Get with default value $theme = settings()->get('ui.theme', 'light');
Nested Retrieval
If you have multiple settings under a prefix, you can retrieve them as an array:
settings()->set('ui.theme', 'dark'); settings()->set('ui.language', 'en'); $ui = settings()->get('ui'); // Returns: ['ui.theme' => 'dark', 'ui.language' => 'en']
Scoped Settings
The real power of this package lies in its ability to handle different scopes (User, Team, Project, etc.).
1. Prepare your Model
Implement the SettingsScope contract and use the HasSettings trait in your model:
namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Hatchyu\Settings\Contracts\SettingsScope; use Hatchyu\Settings\Traits\HasSettings; class User extends Authenticatable implements SettingsScope { use HasSettings; /** * Define the fallback hierarchy for this model. * When a setting isn't found for the user, it checks these scopes in order. */ public function getSettingsFallbackScopes(): array { return [ $this->team, // Check team settings first $this->company, // Then check company settings ]; } }
2. Using Scoped Settings
$user = User::find(1); // Set a setting specifically for this user settings()->scope($user)->set('ui.theme', 'dark'); // Retrieve it (it will look at User -> Team -> Company -> Global) $theme = settings()->scope($user)->get('ui.theme'); // You can also use the trait method directly on the model $user->settings()->set('notifications.email', true); $user->settings()->get('notifications.email');
Technical Details
Setting Definitions
Settings are stored in two parts:
- Definitions: Defines the key, data type, and default value.
- Values: Stores the actual value for a specific scope.
When you use set(), the package automatically creates a definition if it doesn't exist, inferring the data type.
Supported Data Types
The package automatically casts values based on the definition's data_type:
integer,float,boolean,array,object,json,string.
Caching
Settings are cached for 60 minutes (3600 seconds) by default. The cache is automatically cleared for a specific key when that key is updated via set() or removed via forget().
API Reference
SettingsManager / settings() helper
| Method | Description |
|---|---|
scope(SettingsScope $scope) |
Returns a new manager instance focused on the given scope. |
get(string $key, $default = null) |
Retrieve a setting value. |
set(string $key, $value) |
Store a setting value for the current scope. |
has(string $key) |
Check if a setting exists in the current scope chain. |
forget(string $key) |
Remove a setting value for the current scope. |
all(?string $prefix = null) |
Retrieve all settings (optionally filtered by prefix). |
License
The MIT License (MIT). Please see License File for more information.
hatchyu/laravel-settings 适用场景与选型建议
hatchyu/laravel-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.08k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hatchyu/laravel-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hatchyu/laravel-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 53
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-14