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

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

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

petermeijer/laravel-model-settings

Composer 安装命令:

composer require petermeijer/laravel-model-settings

包简介

README 文档

README

A flexible and type-safe settings package for Laravel models. This package allows you to attach settings to any Eloquent model without adding columns to your table. It supports different groups, types (string, boolean, integer, float, array), and custom definitions using Enums or classes.

Features

  • Eloquent Integration: Attach settings to any model via a simple trait.
  • Type Safety: Built-in support for multiple data types with automatic casting.
  • Organization: Group settings to keep things organized.
  • Enum Support: Use PHP Enums to define your settings for better IDE support and type safety.
  • Performance: Eager-loads settings to avoid N+1 query problems.
  • Configurable: Easily customize default groups and type aliases.

Installation

You can install the package via composer:

composer require petermeijer/laravel-model-settings

The service provider will automatically register itself.

You should publish and run the migrations with:

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

You can publish the config file with:

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

Usage

1. Prepare your Model

Add the HasSettingsInterface and HasSettings trait to your model:

use Illuminate\Database\Eloquent\Model;
use PeterMeijer\LaravelModelSettings\Concerns\HasSettings;
use PeterMeijer\LaravelModelSettings\Contracts\HasSettingsInterface;

class User extends Model implements HasSettingsInterface
{
    use HasSettings;
}

2. Basic Usage (String keys)

You can set and get settings using simple string keys. The type will be inferred when setting the value.

$user = User::first();

// Set settings
$user->settings()->set('theme', 'dark');
$user->settings()->set('notifications_enabled', true);
$user->settings()->set('login_count', 5);

// Get settings
$theme = $user->settings()->string('theme'); // 'dark'
$enabled = $user->settings()->boolean('notifications_enabled'); // true
$logins = $user->settings()->integer('login_count'); // 5

// Check existence
if ($user->settings()->has('theme')) {
    // ...
}

3. Using Settings Groups

You can organize settings into groups:

$user->settings('profile')->set('color', 'blue');
$color = $user->settings('profile')->string('color'); // 'blue'

// Or via the fluent API
$user->settings()->group('profile')->set('color', 'red');

4. Advanced Usage (Setting Definitions)

For better type safety and organization, you can define settings using Enums or classes that implement ModelSettingDefinition.

use PeterMeijer\LaravelModelSettings\Contracts\ModelSettingDefinition;

enum UserSetting: string implements ModelSettingDefinition
{
    case Theme = 'theme';
    case Notifications = 'notifications';

    public function key(): string { return $this->value; }
    public function type(): string {
        return match($this) {
            self::Theme => 'string',
            self::Notifications => 'boolean',
        };
    }
    public function group(): string { return 'default'; }
}

// Usage with Enums
$user->settings()->set(UserSetting::Theme, 'light');
$theme = $user->settings()->string(UserSetting::Theme);

5. Available Methods

The settings() accessor provides many helpful methods (inspired by Laravel's InteractsWithData trait):

  • set($key, $value)
  • string($key, $default = '')
  • boolean($key, $default = false)
  • integer($key, $default = 0)
  • float($key, $default = 0.0)
  • array($key)
  • date($key, $format = null, $tz = null)
  • enum($key, $enumClass, $default = null)
  • str($key) (returns Illuminate\Support\Stringable)
  • has($key)
  • missing($key)
  • all()
  • only($keys)
  • except($keys)

Configuration

The configuration file allows you to change the default group and define type aliases:

return [
    'default_group' => 'default',
    
    'type_mappings' => [
        'default' => 'string',
        'types' => [
            'boolean' => ['bool', 'boolean'],
            'integer' => ['int', 'integer'],
            // ...
        ],
    ],
];

Testing

composer test

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-03-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固