maxkalahur/laravel-model-settings
最新稳定版本:1.1.3
Composer 安装命令:
composer require maxkalahur/laravel-model-settings
包简介
This Laravel package allows you to attach settings to Laravel Models, save them in DB and encrypt.
README 文档
README
laravel-model-settings
This Laravel package allows to:
- attach settings to Laravel Models
- store model settings in DB
- encrypt settings in case of need with default Laravel's encryption via APP_KEY
Data types that can be used in CustomSettings:
boolintdoublestringNULLarray
Laravel versions supported: 8+
Instalation
composer require maxkalahur/laravel-model-settings
php artisan vendor:publish --provider="MaxKalahur\LaravelModelSettings\CustomSettingServiceProvider" --tag="migrations"
php artisan migrate
Usage Instructions
Add Trait HasCustomSettings and an attribute array $customSettings to the Model.
use MaxKalahur\LaravelModelSettings\Traits\HasCustomSettings;
class DummyOrg extends Model
{
use HasCustomSettings;
...
private $customSettings = [
'SECRET_KEY',
'ADDRESS',
'HAS_VISITS'
];
// Using in query in scope
public function scopeWithAddress($query)
{
return $query->whereHas('settings', function($q) {
$q->where('key','ADDRESS')->whereNotNull('value');
});
}
}
Methods of custom settings:
setCustomSetting( string $key, $val, bool $is_encrypted)getCustomSetting( string $key )deleteCustomSetting( string $key )getAllCustomSettings()deleteAllCustomSettings()
$org = DummyOrg::create();
$org->setCustomSetting('ADDRESS', 'Some address 59901 US');
$org->getCustomSetting('ADDRESS'); // Returns 'Some address 59901 US'
$org->setCustomSetting('SECRET_KEY', 'da39a3ee5e6b4b', true); // Save with encription
$org->getCustomSetting('SECRET_KEY'); // returns 'da39a3ee5e6b4b'
$org->setCustomSetting('HAS_VISITS', true);
$org->getCustomSetting('HAS_VISITS'); // returns (bool) true
$org->setCustomSetting('HAS_VISITS', 'true');
$org->getCustomSetting('HAS_VISITS'); // returns (string) 'true'
$org->deleteCustomSetting('HAS_VISITS');
$org->getCustomSetting('HAS_VISITS'); // returns NULL
$org->getAllCustomSettings(); // returns all settings as (array)
$org->deleteAllCustomSettings(); // deletes all settings from DB
// Using in scopes
DummyOrg::whereNotNull('name')
->orWhere
->withAddress();
Eagier loading
$org = DummyOrg::with('customSettings')->first();
foreach (range(1, 10) as $i) {
$org->getCustomSetting('ADDRESS'); // no extra calls to DB
}
Encription: ENV APP_KEY is used for encryption, so please keep it safely.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-12