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

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

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

lukasss93/laravel-model-settings

Composer 安装命令:

composer require lukasss93/laravel-model-settings

包简介

Model Settings for your Laravel app

README 文档

README

logo

Laravel Model Settings

version downloads license php

Test Maintainability Test Coverage

Model Settings for your Laravel app

The package requires PHP ^8.0 and follows the FIG standards PSR-1, PSR-2, PSR-4 and PSR-12 to ensure a high level of interoperability between shared PHP.

Bug reports, feature requests, and pull requests can be submitted by following our Contribution Guide.

⚠️ Forked repository

This package is a fork of glorand/laravel-model-settings and contains some breaking changes to make it more flexible and easier to use.

List of changes between this package and the original one:

  • Added ability to initialize settings at model creation
  • Readded Laravel 8 support
  • Converted defaultSettings property to method
  • Converted settingsRules property to method
  • Converted PHPUnit tests to PestPHP tests
  • Renamed Glorand\Model\Settings namespace to Lukasss93\ModelSettings
  • Removed defaultSettings from model_settings.php config file

🚀 Installation

composer require lukasss93/laravel-model-settings

⚙ Publishing the config file

Publishing the config file is optional:

php artisan vendor:publish --provider="Lukasss93\ModelSettings\ModelSettingsServiceProvider"

🌈 Update your Eloquent Models

Your models should use the HasSettingsField or HasSettingsTable trait.

Option 1 - HasSettingsField trait

Run the php artisan model-settings:model-settings-field in order to create a migration file for a table.
This command will create a json field (default name settings, from config) for the mentioned table.
The default name of the field is settings; change the config or env value MODEL_SETTINGS_FIELD_NAME if you want to rewrite the default name (before you run the command!)

You can choose another than default, in this case you have to specify it in you model.

public $settingsFieldName = 'user_settings';

Complete example:

use Lukasss93\ModelSettings\Traits\HasSettingsField;

class User extends Model
{
    use HasSettingsField;

    //define only if you select a different name from the default
    public $settingsFieldName = 'user_settings';
}

Option 2 - HasSettingsTable trait

Run before the command php artisan model-settings:model-settings-table.
The command will copy for you the migration class to create the table where the setting values will be stored.
The default name of the table is model_settings; change the config or env value MODEL_SETTINGS_TABLE_NAME if you want to rewrite the default name (before you run the command!)

use Lukasss93\ModelSettings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;
}

Option 3 - HasSettingsRedis trait

use Lukasss93\ModelSettings\Traits\HasSettingsRedis;

class User extends Model
{
    use HasSettingsRedis;
}

👓 Usage

$user = App\User::first();

Check id the settings for the entity is empty

$user->settings()->empty();

Check settings (exist)

$user->settings()->exist();

Get all model's settings

$user->settings()->all();
$user->settings()->get();

Get a specific setting

$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
//multiple
$user->settings()->getMultiple(
	[
		'some.setting_1',
		'some.setting_2',
	],
	'default value'
);

Add / Update setting

$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
//multiple
$user->settings()->setMultiple([
	'some.setting_1' => 'new value 1',
	'some.setting_2' => 'new value 2',
]);

Check if the model has a specific setting

$user->settings()->has('some.setting');

Remove a setting from a model

$user->settings()->delete('some.setting');
//multiple
$user->settings()->deleteMultiple([
	'some.setting_1',
	'some.setting_2',
]);
//all
$user->settings()->clear();

⬇ Persistence for settings field

In case of field settings the auto-save is configurable.

The default value is true

  • Use an attribute on model
protected $persistSettings = true; //boolean
  • Environment (.env) variable
MODEL_SETTINGS_PERSISTENT=true
  • Config value - model settings config file
'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', true),

If the persistence is false you have to save the model after the operation.

🛠️ Using another method name other than settings()

If you prefer to use another name other than settings , you can do so by defining a $invokeSettingsBy property. This forward calls (such as configurations()) to the settings() method.

🌠 Default settings

You can set default configs for a model:

use Lukasss93\ModelSettings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;
    
    public function defaultSettings(): array
    {
        return [
            'foo' => 'bar',
        ];
    }
}

🎉 Initializing settings at model creation

You can initialize settings at model creation by defining a initSettings property in your model:

use Lukasss93\ModelSettings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;
    
    public bool $initSettings = true;
    
    public function defaultSettings(): array
    {
        return [
            'foo' => 'bar',
        ];
    }
}

🔍 Validation system for settings data

When you're using the set(), apply(), update() methods thrown an exception when you break a rule.
You can define rules on model using settingsRules public method, and the rules array definition is identical with the Laravel default validation rules.

class User extends Model
{
    use HasSettingsTable;

    public function defaultSettings(): array
    {
        return [
            'info' => [
                'email' => 'user@test.com'
                'age' => 27,
            ],
            'language' => 'en',
            'max_size' => 12,
        ];
    }

    public function settingsRules(): array
    {
        return [
            'info' => 'array',
            'info.email' => ['string','email'],
            'info.age' => 'integer',
            'language' => 'string|in:en,es,it|max:2',
            'max_size' => 'int|min:5|max:15',
        ];
    }
}

📃 Changelog

Please see CHANGELOG for more information what has changed recently.

📖 License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-27