maxkalahur / laravel-model-settings
此 Laravel 包允许您将设置附加到 Laravel 模型,将其保存到数据库并加密。
1.0.3
2023-02-24 08:36 UTC
Requires
- php: ^7.3|^8.0
- illuminate/database: ^8.0|^9.0|^10.0
- illuminate/encryption: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.6|^8.0
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-09-24 16:33:31 UTC
README
laravel-model-settings
此 Laravel 包允许您:
- 将设置附加到 Laravel 模型
- 在数据库中存储模型设置
- 加密设置(如有需要),使用默认的 Laravel 加密方法通过 APP_KEY
可以在 CustomSettings 中使用的数据类型
bool
int
double
string
NULL
支持的 Laravel 版本:8+
安装
composer require maxkalahur/laravel-model-settings
php artisan vendor:publish --provider="MaxKalahur\LaravelModelSettings\CustomSettingServiceProvider" --tag="migrations"
php artisan migrate
使用说明
将 Trait HasCustomSettings
和属性 array $customSettings
添加到模型中。
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');
});
}
}
自定义设置的函数:
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();
更快的加载
$org = DummyOrg::with('customSettings')->first();
foreach (range(1, 10) as $i) {
$org->getCustomSetting('ADDRESS'); // no calls to DB
}
加密:使用 ENV APP_KEY
进行加密,请妥善保管。
测试
composer test
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。