承接 hatchyu/laravel-settings 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hatchyu/laravel-settings

最新稳定版本:v1.0.0

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:

  1. Definitions: Defines the key, data type, and default value.
  2. 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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固