rennokki/eloquent-settings
Composer 安装命令:
composer require rennokki/eloquent-settings
包简介
Eloquent Settings allows you to bind key-value pairs to any Laravel Eloquent model. It supports even casting for boolean, float or integer types.
README 文档
README
Eloquent Settings allows you to bind key-value pairs to any Laravel Eloquent model.
🤝 Supporting
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦
🚀 Installation
Install the package:
$ composer require rennokki/eloquent-settings
Publish the config:
$ php artisan vendor:publish --provider="Rennokki\Settings\SettingsServiceProvider" --tag="config"
Publish the migrations:
$ php artisan vendor:publish --provider="Rennokki\Settings\SettingsServiceProvider" --tag="migrations"
🙌 Usage
You can add the HasSettings trait to any Eloquent model:
use Rennokki\Settings\Traits\HasSettings; class User extends Model { use HasSettings; ... }
Adding settings
$user->newSetting('subscribed.to.newsletter', 1); $user->newSetting('subscribed.to.newsletter', true);
By default, settings' values are stored as string. Later, if you try to get them with cast, they will return the value you have initially stored.
If you store 'true' as a string, if you cast it to a boolean, you'll get true.
If you plan to store it with cast type other than string, you can pass an additional third parameter that can be either string, boolean, bool, int, integer, float or double.
$user->newSetting('subscribed.to.newsletter', true, 'bool');
Updating settings
Updating settings can be either to values, cast types or both, depending on what has changed.
$user->updateSetting('subscribed.to.newsletter', false, 'bool');
If you don't specify a cast parameter, it will not change, only the value will change, or viceversa.
Getting settings & values
You can get the Setting instance, not the value using getSetting():
$user->getSetting('subscribed.to.newsletter'); // does not accept a cast
If you plan to get the value, you can use getSettingValue():
$user->getSettingValue('subscribed.to.newsletter'); // true, as boolean $user->getSettingValue('subscribed.to.newsletter', 'int'); // 1, as integer
Remember, when you update or create a new setting, the cast type is stored. By default, next time you don't have to call the cast parameter again because it will cast it the way it was specified on storing.
$user->newSetting('is.cool', true, 'bool'); $user->getSettingValue('is.cool'); // it returns true as boolean
Getting values of not-known settings keys, you will return null.
$user->getSettingValue('subscribed.to.weekly.newsletter'); // null
Deleting a setting
Deleting settings from the database can be done using deleteSetting().
$user->deleteSetting('subscribed.to.newsletter');
To delete all settings, call deleteSettings().
$user->deleteSettings();
🐛 Testing
vendor/bin/phpunit
🤝 Contributing
Please see CONTRIBUTING for details.
🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.
🎉 Credits
rennokki/eloquent-settings 适用场景与选型建议
rennokki/eloquent-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.23k 次下载、GitHub Stars 达 80, 最近一次更新时间为 2018 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「value」 「Settings」 「key」 「eloquent」 「setting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rennokki/eloquent-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rennokki/eloquent-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rennokki/eloquent-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Runn Me! Value Objects Library
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Rinvex Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
统计信息
- 总下载量: 4.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 80
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-05-29