larastash/options
Composer 安装命令:
composer require larastash/options
包简介
Global key-value storage for Laravel based on database.
README 文档
README
The Options class provides a convenient way to interact with option values in a Laravel application.
It allows you to set, get, remove, and check the existence of options. The class also includes methods to manage previously retrieved option values and provides a query builder for the option model.
Requirements
- Laravel ^10 (other version not tested);
- PHP ^8.1;
To support JSON fields in a database, ensure that your chosen database meets the following requirements:
- MySQL: Use MySQL version 5.7.8 or higher. JSON field support was introduced in MySQL starting from version 5.7.8;
- MariaDB: Use MariaDB version 10.2 or higher. JSON field support was introduced in MariaDB starting from version 10.2.
- PostgreSQL: Use PostgreSQL version 9.2 or higher. JSON field support was introduced in PostgreSQL starting from version 9.2;
- SQLite: Use SQLite version 3.9.0 or higher. JSON support and functions were added in SQLite starting from version 3.9.0;
- SQL Server: Use SQL Server 2016 or higher. JSON support was introduced in SQL Server 2016;
- Oracle Database: Use Oracle Database 12c Release 2 (12.2) or higher. JSON support was added in Oracle Database 12c Release 2;
Installation
You can install it using Composer:
composer require larastash/options
Run database migration:
php artisan migrate
✨ Get to work, you're done!
Usage
To use the Options class, make sure to import it into your PHP file:
use Larastash\Options\Option;
Or get Larastash\Options\Option singletone class from app container:
$option = app('option'); $option = app(Option::class);
Also, you can use the option() helper.
Set an Option
You can set the value of an option using the set method:
Option::set('key', 'value');
This method will update the value of the option with the specified key. If the option doesn't exist, it will be created.
You can also specify a time-to-live (TTL) for the option. The TTL determines how long the option will be cached. If a TTL is specified, the option will be stored in the cache in addition to being persisted in the database:
// Set an option with a TTL of 1 hour Option::set('key', 'value', 3600); Option::set('key', 'value', now()->addHour());
Get an Option
To retrieve the value of an option, use the get() method:
$value = Option::get('key');
You can also specify a default value to return if the option is not found:
$value = Option::get('key', 'default');
If a TTL is specified and the option is found in the cache, the cached value will be returned. Otherwise, the option will be retrieved from the database.
// Retrived value will be cached for 1 hour $value = Option::get('key', 'default', 3600); $value = Option::get('key', 'default', now()->addHour());
If TTL is not null, then get the fresh value from the database, ignoring the cached value.
Remove an Option
You can remove an option using the remove method:
Option::remove('key');
This will delete the option from the database and remove it from the cache, if it exists.
Check if an Option Exists
You can check if an option exists using the exists() method:
if (Option::exists('key')) { // Option exists } else { // Option does not exist }
The exists method returns true if the option with the specified key exists in the database; otherwise, it returns false.
Get All Option Values
To retrieve all option values, you can use the all method:
$options = Option::all();
The all method returns a Illuminate\Database\Eloquent\Collection of all option values.
Querying the Option Model
You can access the query builder of the option model using the query method:
$query = Option::query();
The query method returns an instance of the Illuminate\Database\Eloquent\Builder class that can be used to build custom queries on the option model.
Advanced Usage
If you need more advanced querying capabilities, you can use the query() method to get a query builder instance for the option model. This allows you to perform complex database queries on the option model:
$query = Option::query(); $query->where('key', 'like', 'prefix%'); $options = $query->get();
This example retrieves all options whose keys start with a specific prefix.
Helpers
option()
The option() function provides a convenient way to interact with option values in a Laravel application. It acts as a wrapper around the Options class methods and simplifies the retrieval and setting of options.
Parameters:
$key(string|array|null): The key of the option to retrieve or an array containing the key and value to set. If set tonull, it will return an instance of theOptionclass.$default(mixed): (Optional) The default value to return if the option does not exist. This parameter is only used when retrieving an option value.$ttl(DateInterval|DateTimeInterface|int|null): (Optional) The default value to return if the option does not exist. This parameter is only used when retrieving an option value.
Return Value:
The function returns the value of the option with the specified key if a key is provided.
If an array is provided, it sets the value of the option with the specified key.
If no arguments are provided, it returns an instance of the Option class.
Usage Examples:
Get an Option:
$value = option('key', 'default value'); $value = option('key', 'default value', ttl: 3600); $value = option('key', 'default value', ttl: now()->addHour());
This retrieves the value of the option with the specified key. If the option does not exist, it returns the provided default value.
Set an Option:
option(['key' => 'new value']); option(['key' => 'new value'], ttl: 3600); option(['key' => 'new value'], ttl: now()->addHour()); // or as array list option(['key', 'new value']); option(['key', 'new value'], ttl: 3600); option(['key', 'new value'], ttl: now()->addHour());
This sets the value of the option with the specified key. If the option does not exist, it will be created.
Access the Option class:
$option = option(); option()->set(...); option()->get(...); option()->remove(...); option()->exists(...); option()->query(...); // and etc...
This returns an instance of the Option class, allowing you to perform advanced operations.
Testing
$ composer test
Contributing
If you find any issues or have suggestions for improvement, please feel free to contribute by creating a pull request or submitting an issue.
Credits
License
The MIT License (MIT). Please see License File for more information.
larastash/options 适用场景与选型建议
larastash/options 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「options」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 larastash/options 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 larastash/options 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 larastash/options 相关的其它包
同方向 / 同关键字的高下载量 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.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-16