glorand/laravel-model-settings
Composer 安装命令:
composer require glorand/laravel-model-settings
包简介
Model Settings for your Laravel app
README 文档
README
Model Settings for your Laravel app
The package requires PHP 8.1+ 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.
Table of contents
- Installation
- Updating your Eloquent Models
- Default Settings
- Usage
- Check if the settings for the entity is empty (exist)
- Check settings (exist)
- Get all model's settings
- Get a specific setting
- Add / Update setting
- Check if the model has a specific setting
- Remove a setting from a model
- Persistence
- Using another method name other than
settings() - Validation system for settings data
- Changelog
- Contributing
- License
Installation
$ composer require glorand/laravel-model-settings
{
"require": {
"glorand/laravel-model-settings": "^4.0"
}
}
Env (config) variables (.env file)
Default name for the settings field - when you use the HasSettingsField
MODEL_SETTINGS_FIELD_NAME=settings
Default name for the settings table - when you use the HasSettingsTable
MODEL_SETTINGS_TABLE_NAME=model_settings
Updating your Eloquent Models
Your models should use the HasSettingsField or HasSettingsTable trait.
Option 1 - HasSettingsField trait
Run the command below in order to create a migration file for a table.\
php artisan model-settings:model-settings-field
This command will create a json field (default name settings, from config) for the mentioned table.
You can choose another than default, in this case you have to specify it in you model.
public $settingsFieldName = 'user_settings';
Complete example:
use Glorand\Model\Settings\Traits\HasSettingsField; class User extends Model { use HasSettingsField; //define only if you select a different name from the default public $settingsFieldName = 'user_settings'; //define only if the model overrides the default connection protected $connection = 'mysql'; }
Option 2 - HasSettingsTable trait
Run the command below to create the settings table.
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 Glorand\Model\Settings\Traits\HasSettingsTable; class User extends Model { use HasSettingsTable; }
Option 3 - HasSettingsRedis trait
use Glorand\Model\Settings\Traits\HasSettingsRedis; class User extends Model { use HasSettingsRedis; }
Default settings
You can set default configs for a table in model_settings.php config file
return [ // start other config options // end other config options // defaultConfigs 'defaultSettings' => [ 'users' => [ 'key_1' => 'val_1', ] ] ];
Or in your model itself:
use Glorand\Model\Settings\Traits\HasSettingsTable; class User extends Model { public $defaultSettings = [ 'key_1' => 'val_1', ]; }
Please note that if you define settings in the model, the settings from configs will have no effect, they will just be ignored.
Usage
$user = App\User::first();
Check if 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.
Validation system for settings data
When you're using the set() or apply()|update() methods thrown an exception when you break a rule.
You can define rules on model using $settingsRules public property, and the rules array definition is identical with
the Laravel default validation rules. (see Laravel rules)
class User extends Model { use HasSettingsTable; public array $defaultSettings = [ 'user' => [ 'name' => 'Test User', 'email' => 'user@test.com' 'age' => 27, ], 'language' => 'en', 'max_size' => 12, ]; // settings rules public array $settingsRules = [ 'user' => 'array', 'user.email' => [ 'string', 'email', ], 'user.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.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see LICENSE for more information.
Related Stuff
glorand/laravel-model-settings 适用场景与选型建议
glorand/laravel-model-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16M 次下载、GitHub Stars 达 911, 最近一次更新时间为 2019 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Settings」 「laravel-settings」 「glorand」 「laravel-model-settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 glorand/laravel-model-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 glorand/laravel-model-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 glorand/laravel-model-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Persistent settings with caching in Laravel.
Notification system for laravel
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
Per-model preferences for Laravel with defaults and validation.
统计信息
- 总下载量: 1.16M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 911
- 点击次数: 9
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-08
