rpillz/laravel-settings

存储在数据库中的Laravel应用设置

dev-main 2023-07-03 19:47 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包将设置和获取您的Laravel应用程序数据库中的键值设置。它最初是为与多租户应用程序一起使用而设计的,因此设置存储在数据库中,而不是标准的配置文件中。

可能还有其他包执行类似的功能。您可能应该使用其中之一。 ;)

安装

您可以通过composer安装此包

composer require rpillz/laravel-settings

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="settings-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="settings-config"

这是已发布配置文件的内容

return [
    // these default settings will be used if there is nothing saved in the database using the same key.
    'defaults' => [

        'default-key' => 'Default Value',
        'is-this-true' => true,

    ]
];

使用方法

主要使用方式是通过外观。

Settings::get('default-key'); // returns 'Default Value' from the config file.

Settings::set('default-key', 'My New Value'); // updates this setting in the database.

// Beware of cached values
Settings::get('default-key'); // will still return the original 'Default Value'.

// Get the latest value
Settings::fresh('default-key');
Settings::get('default-key', true); // passing a true with get() is the same as fresh()

您可以使用不同的类型转换添加设置值。

Settings::set('default-key', 'My New Value', 'string'); // string is default. What goes in is what comes out.

Settings::set('default-key', 'My New Value', 'array'); // convert string into single array value

Settings::set('numbers', 'one, two, three', 'csv'); // convert csv string into array

Settings::set('is_active', 1, 'boolean'); // convert value into boolean

您可以删除设置。

Settings::forget('this-setting'); // temporarily nulls in the settings cache (for current page load only)

Settings::delete('this-setting'); // removes setting from the cache and the database.

使用流畅的for()函数为特定模型设置事物。您可以使用此功能进行唯一设置或覆盖默认值。

Settings::set('this-setting-is', 'on base');

Settings::for($model)->set('this-setting-is', 'on model');

请注意,通过将true作为第二个参数传递,可以将for()函数变为“粘性”,之后所有设置都将保持该设置模型。

Settings::for($model, true)->set('this-setting-is', 'on model');

Settings::set('this-setting-is', 'still on model');

Settings::resetModel(); // to clear out the sticky model.

测试

composer test

更新日志

有关最近更改的更多信息,请参阅更新日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞,请参阅我们的安全策略

致谢

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件