maxkalahur/laravel-model-settings 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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

Latest Version on Packagist Run Tests Check & fix styling

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:

  • bool
  • int
  • double
  • string
  • NULL
  • array

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固