softinklab/laravel-keyvalue-storage

使用数据库或JSON文件存储的Laravel键值存储

v1.8 2022-03-27 06:08 UTC

This package is auto-updated.

Last update: 2024-09-27 12:17:30 UTC


README

laravel keyvalue storage

Latest Version on Packagist Total Downloads Software License Build Status

介绍

Laravel键值存储是一个简单且易于使用的包,可在Laravel中全局存储键值数据。此包支持数据库和JSON文件作为存储方法。此包还附带辅助函数,可简化您在代码和Blade模板中访问键值存储。

安装

您可以通过composer安装此包

composer require softinklab/laravel-keyvalue-storage

设置

此包支持Laravel 5.5及更高版本的自动发现功能,因此如果您使用的是Laravel 5.5及以上版本,请跳过这些设置说明。

config/app.php 中添加以下内容:

1 - Service Provider 到 providers 数组中

SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider::class,

2 - 类别名到 aliases 数组中

'KVOption' => SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption::class,

3 - 发布配置文件

php artisan vendor:publish --provider="SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider"

4 - 迁移数据库表

php artisan migrate

配置

您可以在 config/kvstorage.php 中更改设置。

示例:数据库存储

'method' => 'database',
'table_name' => 'kv_storage',

示例:文件存储

'method' => 'file',
'disk' => 'local',
'path' => 'kvstorage/',

用法

使用门面(FACADE)

use SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption;

// Check is the option exists. Return true if found.
KVOption::exists('key');

// Get the option value.
KVOption::get('key');

// Get the option value. Default value is optional. If option not found default value is returned.
KVOption::get('key', 'default value');

// Add new option. Comment is optional. Comment is only working in Database Mode.
KVOption::set('key', 'value', 'comment');

// Add multiple options.
// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']]
KVOption::setArray('array');

// Increment the value of a given key and return it as integer. Factor is used to determine the step. Default is one.
KVOption::increment('key', 'factor');

// Decrement the value of a given key and return it as integer. Factor is used to determine the step. Default is one.
KVOption::decrement('key', 'factor');

// Delete an option
KVOption::remove('key');

使用辅助函数

// Check is the option exists. Return true if found.
kvoption_exists('someKey');

// Get the option value.
kvoption('key');

// Get the option value. Default value is optional. If option not found default value is returned.
kvoption('key', 'default value');

// Add new option. Comment is optional. Comment is only working in Database Mode.
kvoption(['key','value', 'comment']);

// Add multiple options.
// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']]
kvoption('array');

Blade模板

您可以使用 kvoptions() 辅助函数在Blade模板中访问键值。

{{kvoption('key')}}

控制台

有一些控制台命令可以执行操作。

创建选项

php artisan kvoption:create {key} {value} --comment={comment}

注释是可选的。

删除选项

php artisan kvoption:delete {key}

鸣谢

此包受到以下灵感的启发:

贡献

欢迎贡献!请参阅 CONTRIBUTING 以获取详细信息。

许可证

Laravel键值存储是开源软件,根据Apache 2.0许可证授权。