merodiro/settings

Easy global/user settings

v1.0 2018-04-10 19:38 UTC

This package is not auto-updated.

Last update: 2022-09-14 14:50:38 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

laravel easy key => value global/user settings

Install

Via Composer

$ composer require merodiro/settings

publish config through Optional: only if you want to edit cache configurations

$ php artisan vendor:publish --provider=Merodiro\Settings\SettingsServiceProvider

Setup a Model

use Merodiro\Settings\HasSettings;

class User extends Model
{
    use HasSettings;
    ...
}

Usage

Set settings

creates a record if the key doesn't exist or update it if the key exists

in addition to updating the cache

// Global Settings
Settings::set('key', 'value');
Settings::set('key', 'another value');

// User Settings
$user->setSettings('key', 'value');
$user->setSettings('key', 'another value');

Get value from settings

Returns its value if it exists or the second parameter if it doesn't exist

// Global Settings
$name = Settings::get('site-name');
$value = Settings::get('key', 'default');

// User Settings
$user->getSettings('site-name');
$user->getSettings('key', 'value');

Delete key from settings

Remove the setting with the given key in addition to removing it from the cache

// Global Settings
Settings::forget('key');

// User Settings
$user->forgetSettings('key');

Delete all settings

Delete all the settings in addition to removing them from the cache

// Global Settings
Settings::flush();

// User Settings
$user->flushSettings();

Get all settings

Returns all settings stored in key => value array

// Global Settings
$settings = Settings::all();

// User Settings
$settings = $user->allSettings();

Artisan Commands

Cache all settings

Caches all settings for the duration that has been set in settings.php config file

you can set the duration to a high number or schedule the command to run often to get the best value of it

# Global settings only
php artisan settings:cache

# Global and User Settings
php artisan settings:cache --model=App/User

Clear cache for all settings

# Global settings only
$ php artisan settings:clear

# Global and User Settings
$ php artisan settings:clear --model=App/User

Blade Directives

Get value from blade template

<h1>@settings('site-name')</h1>
<h1>@settings('site-name', 'default name')</h1>

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security-related issues, please email merodiro@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.