承接 dragon-code/laravel-model-settings 相关项目开发

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

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

dragon-code/laravel-model-settings

Composer 安装命令:

composer require dragon-code/laravel-model-settings

包简介

Model Settings for your Laravel application

README 文档

README

model settings

Stable Version Total Downloads License

Store settings for individual Eloquent models, with optional defaults shared by models of the same type.

Use this package when each model needs its own settings, but should fall back to shared values when a model value is missing.

Installation

Install the package via Composer:

composer require dragon-code/laravel-model-settings

Publish the config and migration, then run migrations:

php artisan vendor:publish --tag="model-settings"
php artisan migrate

Quick Start

Add the HasSettings trait to a model:

use DragonCode\LaravelModelSettings\Concerns\HasSettings;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasSettings;
}

Use settings on a saved model:

$user = User::query()->findOrFail(123);

$user->settings()->set('timezone', 'UTC');
$user->settings()->set('notifications', ['email' => true]);

$user->settings()->get('timezone');      // 'UTC'
$user->settings()->get('notifications'); // ['email' => true]
$user->settings()->get('missing');       // null

$user->settings()->all();                // Illuminate\Support\Collection

$user->settings()->forget('timezone');
$user->settings()->get('timezone');      // null

Calling set() with a blank value removes the model setting. Blank values include null, an empty string, and an empty array.

Default Settings

Default settings are fallback values shared by models of the same type:

(new User)->defaultSettings()->set('timezone', 'UTC');

$user->settings()->get('timezone'); // 'UTC'

$user->settings()->set('timezone', 'Europe/Paris');
$user->settings()->get('timezone'); // 'Europe/Paris'

$user->settings()->forget('timezone');
$user->settings()->get('timezone'); // 'UTC'

Default settings are stored with the model morph class and item_id = 0.

API

Method Returns Description
all() Collection Returns defaults merged with model settings. Model values win.
get(UnitEnum|string|int $key) mixed Returns the model value, then the default value, then null.
set(UnitEnum|string|int $key, mixed $value) void Creates, updates, or removes a model setting.
forget(UnitEnum|string|int $key) void Removes a model setting.

Setting Keys

Keys can be strings, integers, or PHP enums:

enum UserSetting: string
{
    case Timezone = 'timezone';
}

$user->settings()->set(UserSetting::Timezone, 'UTC');
$user->settings()->get(UserSetting::Timezone); // 'UTC'

Backed enums, unit enums, strings, and integers are supported.

Configuration

After publishing, edit config/model-settings.php:

Option Environment variable Default
model - DragonCode\LaravelModelSettings\Models\Settings::class
connection MODEL_SETTINGS_DATABASE_CONNECTION env('DATABASE_CONNECTION')
table MODEL_SETTINGS_DATABASE_TABLE settings

The migration stores settings in one table with item_type, string item_id, key, and JSONB payload. Each setting is unique by item_type, item_id, and key. The string item_id column supports integer and UUID model keys.

Testing

composer test
composer test:coverage

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a security bug, mail helldar@dragon-code.pro instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: Python

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固