nickdekruijk/settings

A basic cache enabled Setting model, migration and helper for your Laravel project

1.2.0 2022-03-10 11:42 UTC

This package is auto-updated.

Last update: 2024-04-10 16:33:38 UTC


README

Latest Stable Version Latest Unstable Version Monthly Downloads Total Downloads License

Settings

A basic cache enabled Setting model, migration and helper for your Laravel project. It uses a database to store settings for your application. When retrieving settings they are stored in the Laravel cache to prevent unnecessary database queries.

Installation

To install the package use

composer require nickdekruijk/settings

Configuration

If you don't like the default configuration options publish the config file and change the settings.php file in your Laravel app/config folder.

php artisan vendor:publish --tag=config --provider="NickDeKruijk\Settings\ServiceProvider"

Usage

Retrieving settings

If the setting table is created (run php artisan migrate) and you added your first setting you can use setting('key'); from anywhere in your application. The setting helper also accepts a default value in case the key isn't present in the database like setting('key', 'defaultvalue');. You can call NickDeKruijk\Settings\Setting::get($key) too.

Retrieving a setting as array

When you have a setting with a value like this:

facebook = https://www.facebook.com/
twitter = https://twitter.com/
instagram = https://instagram.com/

You can have it returned as an array using setting('key', null, '=') which will return this array

[
  "facebook" => "https://www.facebook.com/",
  "twitter" => "https://twitter.com/",
  "instagram" => "https://instagram.com/"
]

Adding settings

To update of create a new setting you use the setting helper with an array like setting(['key' => 'value']); or call NickDeKruijk\Settings\Setting::set([$key => $value]);. To include description you can use setting(['key' => ['value' => 1, 'description' => 'string']]);. The setting will be added to the database or updated if it already exists. The Setting Model also triggers an event on updated and created to store the new value in the cache.

License

Settings is open-sourced software licensed under the MIT license.