mxrck/laravel-dboptions
Composer 安装命令:
composer require mxrck/laravel-dboptions
包简介
Create database based options for your laravel projects
README 文档
README
Use the database to store key/value options, with preload function and context objects with fallback to global options
Install
composer require mxrck/laravel-dboptions
Usage
There are two ways to use the options store, by facade or helpers
Simple facade usage
If you only need a database key/value to store data, then you can use the simplest way
// Get an option from the database or fallback to default Options::get( 'somekey', 'default value' ); // if you need to reload from database then pass a third boolean argument with true // Update or create (if not exists) an option Options::update( 'somekey', 'somevalue' ); // If you want to autoload the option then pass a third array argument like ['autoload' => true] // Check if an option is already stored in the database Options::exists( 'somekey' ); // Delete an option from the database Options::remove( 'somekey' ); // Get all options as array Options::all(); /* this method return an array like this */ $all_options = [ 'somekey' => [ 'value' => 'THE_VALUE_STORED', 'autoload' => false, 'public' => false ], ... ]; // Get all public options as array Options::public(); // same as before, but only public options
Simple helper usage
// Get options instance $options = option(); /* With $options you have all facade methods available to use */ /* * options()->get(...); options()->update(...); options()->exists(...); ... */ // Get and option from database or fallback to default option('somekey') // Update or create (if not exists) an option option_update( 'somekey', 'somevalue' ); // Check if option exists option_exists( 'somekey' ); // Delete an option option_remove( 'somekey' );
Context usage
This feature was requested by @atxy2k
If you need a more advanced usage, like set options per user, with fallback to default option system or fallback to a default value, then you can use an option context. This contexts make use of polymorphic relations to create specific options, you can make any model to be optionable if you wish.
You need to implement the OptionableInterface, and use the OptionableTrait in the model you want to be optionable like this.
// User.php use Illuminate\Database\Eloquent\Model; class User extends Model implements OptionableInterface { use OptionableTrait; }
In the case you customize your morph map, you need to override and extra method in your model
// Provider use Illuminate\Database\Eloquent\Relations\Relation; Relation::morphMap([ 'user' => 'App\User' ]); // User.php use Illuminate\Database\Eloquent\Model; class User extends Model implements OptionableInterface { use OptionableTrait; public function getType(): string { return 'user'; } }
now you can use the facade or helpers with an optionable context like this
$user = User::find(10); $user->option( 'somekey' ); // By default, from an optionable instance you can call options with the optionable context // Or you can use the facade and helper with custom context $value = Option::context( option_context( $user ) )->get( 'somekey' ); // Or $value = Option::context( Context::make( $user ) )->get( 'somekey' ); // Or $value = Option::context( $user )->get( 'somekey' ); // Or $value = option( option_context( $user ) )->get( 'somekey' ); // Or $value = option( $user )->get( 'somekey' );
Some examples
option( $user )->get( 'some_option' ); // return null option( 'some_option'); // return null option( $user )->update( 'some_option', 'user_value' ); // return 'user_value' option( $user )->get( 'some_option' ); // return 'user_value' option()->update( 'some_option', 'system_value' ); // return 'system_value' option( 'some_option' ) // return 'system_value' echo 'System: ' option( 'some_option' ) . ' User: '. option( $user )->get( 'some_option' ); // System: system_value User: user_value // Fallback example: option()->update( 'color', '#fffff' ); option( option_context( $user ) )->get( 'color' ); // return null option( option_context( $user, true ) )->get( 'color' ); // return #ffffff // Or using your model: $user->optionFallback( 'color' ); // return #ffffff // By default, context fallback is false
There are some hidden features undocumented yet, but the basic usage is here
Console
Create or update an option
php artisan option:update {KEY} {VALUE}
Get an option
php artisan option:get {KEY}
List all current options
php artisan option:all
Testing
composer test
WIP
There are no Context testing yet
Contributing
Thanks in advance, for all the contributions
Support me
You can follow me on Twitter, buy me a coffee or support me on Patreon
License
The MIT License (MIT). Please see License File for more information.
mxrck/laravel-dboptions 适用场景与选型建议
mxrck/laravel-dboptions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.56k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「options」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mxrck/laravel-dboptions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mxrck/laravel-dboptions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mxrck/laravel-dboptions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Lightweight configuration interfaces and utilities
Store your language lines in the database, yaml or other sources
This extension provide application configuration support stored in database.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.
统计信息
- 总下载量: 1.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2019-02-21