定制 bhhaskin/laravel-settings 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bhhaskin/laravel-settings

Composer 安装命令:

composer require bhhaskin/laravel-settings

包简介

Typed, polymorphic key-value settings for Laravel models.

README 文档

README

Typed, polymorphic key-value settings for Laravel models.

Settings are stored in a dedicated settings table with a type column so values round-trip as the right PHP type (boolean, integer, float, string, array, json, datetime) rather than opaque JSON blobs. The relation is polymorphic, so any model — not just User — can have settings.

Installation

composer require bhhaskin/laravel-settings

Publish and run the migration:

php artisan vendor:publish --tag=settings-migrations
php artisan migrate

Optionally publish the config:

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

Setup

Add the HasSettings trait to any model that should own settings:

use Bhhaskin\LaravelSettings\Concerns\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSettings;
}

Usage

$user->setSetting('theme', 'dark');               // type detected as string
$user->setSetting('notifications.email', true);   // type detected as boolean
$user->setSetting('digest.hour', 8);              // integer
$user->setSetting('tags', ['news', 'tips']);      // array

$user->getSetting('theme');                       // 'dark'
$user->getSetting('notifications.email');         // true
$user->getSetting('missing', 'fallback');         // 'fallback'

$user->hasSetting('theme');                       // true
$user->forgetSetting('theme');
$user->allSettings();                             // ['notifications.email' => true, ...]

Forcing a type

Pass a third argument when you want to override auto-detection:

use Bhhaskin\LaravelSettings\Enums\SettingType;

$user->setSetting('flag', 'yes', SettingType::Boolean);  // stored as true
$user->setSetting('opens_at', '2026-05-01T09:00:00Z', SettingType::Datetime);

Declaring defaults and types

Predeclare keys in config/settings.php to skip passing types and get defaults for unset keys:

'definitions' => [
    'theme'                => ['type' => 'string',  'default' => 'system'],
    'notifications.email'  => ['type' => 'boolean', 'default' => true],
    'digest.hour'          => ['type' => 'integer', 'default' => 8],
],

Keys outside this list still work — definitions are opt-in sugar, not a schema.

Runtime-configurable defaults

Config defaults ship with your code. When you also need defaults an admin can change at runtime, use the setting_defaults table. Defaults can be global or scoped to a specific owner morph class:

use Bhhaskin\LaravelSettings\SettingsManager;
use Bhhaskin\LaravelSettings\Facades\Settings;

// Global default — applies to every model that owns settings
Settings::setDefault('theme', 'sunrise');

// Default scoped to one owner class
Settings::setDefault('theme', 'midnight', null, User::class);

Settings::getDefault('theme');               // 'sunrise'
Settings::getDefault('theme', User::class);  // 'midnight'
Settings::forgetDefault('theme', User::class);

getSetting() resolves in this order:

  1. The owner's own stored value
  2. DB default scoped to the owner's morph class
  3. Global DB default
  4. The $default argument
  5. Config definitions[$key]['default']
  6. null

Use allSettingsWithDefaults() when you need the merged view:

$user->allSettingsWithDefaults();
// ['theme' => 'midnight', 'digest.hour' => 8, ...]

allSettings() is unchanged — it still returns only values stored for the owner.

Attaching settings to other models

Because the relation is polymorphic, you can add HasSettings to workspaces, teams, or any other model and call the same API. Each model's settings are scoped by owner_type / owner_id.

Testing

composer test

bhhaskin/laravel-settings 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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