pico/settings 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

pico/settings

Composer 安装命令:

composer require pico/settings

包简介

A Laravel package for key-value settings, supporting user-specific and model-specific configurations.

README 文档

README

A lightweight Laravel package for managing key-value settings. Supports global, user-specific, and model-specific scopes with optional JSON file caching.

Requirements: PHP 8.3+ · Laravel 11 or 12 or 13

Installation

composer require pico/settings

Publish the config file:

php artisan vendor:publish --tag=pico-settings-config

Generate and run the migration:

php artisan pico:settings-table-migration
php artisan migrate

This creates a timestamped create_settings_table migration in your database/migrations/ directory.

If you want to customise the stub before generating, publish it first:

php artisan vendor:publish --tag=pico-settings-stubs

The stub will be placed at stubs/pico-settings/create_settings_table.stub and pico:settings-table-migration will use it automatically.

Configuration

config/pico-settings.php

return [

    // The table name used for the users foreign key
    'user_table' => 'users',

    'cache' => [
        // Enable JSON file caching (recommended for production)
        'enabled' => true,

        // Directory where per-scope JSON cache files are stored
        'path' => storage_path('app/pico-settings'),
    ],

];

Usage

Facade

use Pico\Settings\Facades\Settings;

Helper function

settings()

Global settings

// Set a single value
Settings::set('maintenance_mode', 'false');

// Set multiple values at once
Settings::set(['theme' => 'dark', 'locale' => 'en']);

// Get a single value (with optional default)
Settings::get('theme');            // 'dark'
Settings::get('missing', 'light'); // 'light'

// Get multiple values at once
Settings::get(['theme', 'locale']);
// ['theme' => 'dark', 'locale' => 'en']

User-specific settings

$user = auth()->user();

Settings::for($user)->set('locale', 'fr');
Settings::for($user)->set(['locale' => 'fr', 'timezone' => 'Europe/Paris']);

Settings::for($user)->get('locale');             // 'fr'
Settings::for($user)->get('missing', 'en');      // 'en'
Settings::for($user)->get(['locale', 'timezone']);
// ['locale' => 'fr', 'timezone' => 'Europe/Paris']

Model-specific settings

use App\Models\Post;

Settings::model(Post::class)->set('per_page', '20');
Settings::model(Post::class)->get('per_page'); // '20'

You can also pass a model instance:

$post = Post::find(1);
Settings::model($post)->set('featured', 'true');

Combined scope (user + model)

Settings::for($user)->model(Post::class)->set('view', 'grid');
Settings::for($user)->model(Post::class)->get('view'); // 'grid'

// Scopes are isolated — other scopes are unaffected
Settings::for($user)->get('view');              // null
Settings::model(Post::class)->get('view');      // null

Using the helper

settings()->for($user)->model(Post::class)->get('view', 'list');
settings()->for($user)->set(['locale' => 'en', 'timezone' => 'UTC']);

Deleting settings

// Delete a single key
Settings::delete('theme');

// Delete multiple keys
Settings::delete(['theme', 'locale']);

// Delete all keys in the current scope
Settings::delete();

// Delete within a user scope
Settings::for($user)->delete('locale');

// Delete within a combined user + model scope
Settings::for($user)->model(Post::class)->delete('view');

Deleting only affects the targeted scope — other scopes are left untouched.

Caching

When caching is enabled, settings are stored in JSON files under storage/app/pico-settings/ (configurable). Each scope gets its own file:

Scope File
Global global.json
User only user_1.json
Model only model_posts.json
User + model user_1_model_posts.json

On every set() call, the database is written first and then the cache file is rebuilt. On the first get() after boot (cache miss), the full scope is loaded from DB and written to the cache.

Clearing the cache

To delete all JSON cache files at once:

php artisan pico:clear

To disable caching (e.g. in tests):

// config/pico-settings.php
'cache' => [
    'enabled' => false,
],

Database schema

Column Type Notes
id bigint Primary key
user_id bigint (nullable) FK → users table
model string (nullable) Fully-qualified class name
key string Setting key
value longText Setting value
created_at timestamp
updated_at timestamp

A unique constraint is enforced on (user_id, model, key).

pico/settings 适用场景与选型建议

pico/settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「configuration」 「laravel」 「Settings」 「key-value」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pico/settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pico/settings 我们能提供哪些服务?
定制开发 / 二次开发

基于 pico/settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-10