Skip to content

RPillz/laravel-settings

Repository files navigation

Laravel app settings stored in the database

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

This package will set and get key-value settings from your Laravel app database. It was originally designed to work with a multi-tenant app, which is why the settings were stored in a database and not the standard config files.

There are probably other packages which do a similar thing. You should probably use one of those. ;)

Installation

You can install the package via composer:

composer require rpillz/laravel-settings

You can publish and run the migrations with:

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

You can publish the config file with:

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

This is the contents of the published config file:

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,

    ]
];

Usage

Primary usage is through a Facade.

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()

You can add setting values with different type casts.

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

You can remove settings.

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.

Use the fluent for() function to set things for a specific model. You may use this for unique settings, or to override defaults.

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

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

Note that the for() function can be made "sticky" by passing true as the second argument and all uses of settings after will maintain the set model.

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.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

About

Laravel app settings stored in the database

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages