larapacks / setting by stevebauman

Persistent Laravel configuration settings.
5,694
5
3
Package Data
Maintainer Username: stevebauman
Package Create Date: 2016-05-09
Package Last Update: 2023-03-07
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-15 15:19:20
Package Statistics
Total Downloads: 5,694
Monthly Downloads: 9
Daily Downloads: 0
Total Stars: 5
Total Watchers: 3
Total Forks: 2
Total Open Issues: 0

Setting

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version License

Description

Setting is an easy, encrypted & cached, database key => value store for your laravel application.

Installation

Insert Setting in your composer.json file:

"larapacks/setting": "1.0.*"

Then run composer update.

Insert the service provider in your config/app.php file:

Larapacks\Setting\SettingServiceProvider::class,

Insert the facade in your aliases array in your config/app.php file (only if you're going to utilize it):

'Setting' => Larapacks\Setting\Facades\Setting::class

Once that's complete, publish the migration and configuration file using:

php artisan vendor:publish --tag="setting"

Then run php artisan migrate.

Usage

Note: All usage below can be accessed via the helper method setting().

Setting a value:

Setting::set('key', 'value');

Setting multiple values:

Setting::set([
    'key.1' => 'value',
    'key.2' => 'value',
    'key.3' => 'value',
]);

Retrieving a value:

$value = Setting::get('key.1');

dd($value); // Returns 'value'

Retrieving a value or return default value if it doesn't exist:

$value = Setting::get('non-existent-key', 'default');

dd($value); // Returns 'default'

Retrieving the Setting model for a particular key:

$model = Setting::find('key.1');

dd($model); // Reurns instance of Model (your configured model).

Retrieving all keys with values:

Setting::set([
    'key.1' => 'value',
    'key.2' => 'value',
    'key.3' => 'value',
]);

$settings = Setting::all();

dd($settings);

// Returns:
// array [
//  'key.1' => 'value',
//  'key.2' => 'value',
//  'key.3' => 'value',
// ]

Retrieving the your configured Setting model:

$model = Setting::model();

$setting = new $model();

$setting->key = 'key';
$setting->value = 'value';

$setting->save();

Determining if a setting exists:

if (Setting::has('key')) {
    // The setting exists.
}

Flipping a boolean setting:

Setting::set('notifications', true);

// Disable notifications.
Setting::flip('notifications');

dd(Setting::get('notifications')); // Returns false.

// Enable notifications.
Setting::flip('notifications');

dd(Setting::get('notifications')); // Returns true.

// Default flip setting:
Setting::flip('new-key');

dd(Setting::get('new-key')); // Retuns true.

Enabling a boolean setting:

Setting::set('notifications', false);

Setting::enable('notifications');

dd(Setting::get('notifications')); // Returns true.

Disabling a boolean setting:

Setting::set('notifications', true);

Setting::disable('notifications');

dd(Setting::get('notifications')); // Returns false.

Using your own model

To use your own model, change the model configuration option in your config/settings.php file.

When you create your own model, be sure to include the trait: Larapacks\Setting\Traits\SettingTrait:

namespace App;

use Larapacks\Setting\Traits\SettingTrait;

class Setting extends Model
{
    use SettingTrait;
}

Encryption

Encryption can be enabled or disabled in the published configuration file. By default, it is enabled.

Encryption is performed by laravel's included helper methods encrypt() and decrypt().

You can enable or disable encryption at any time, however upon disabling encryption you will receive the raw encrypted string for settings that have previously been encrypted.