PHP code example of philiprehberger / laravel-settings

1. Go to this page and download the library: Download philiprehberger/laravel-settings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

philiprehberger / laravel-settings example snippets


return [
    'table' => 'settings',

    'cache' => [
        'enabled' => true,
        'key'     => 'app_settings',
        'ttl'     => 3600,          // seconds; null = forever
    ],

    'defaults' => [
        // 'app.timezone' => 'UTC',
    ],
];

use PhilipRehberger\Settings\Facades\Settings;

// Store a value (type is auto-detected)
Settings::set('app.name', 'My Portal');
Settings::set('pagination.per_page', 25);
Settings::set('feature.dark_mode', true);
Settings::set('allowed.ips', ['127.0.0.1', '10.0.0.1']);

// Retrieve
Settings::get('app.name');                         // 'My Portal'
Settings::get('missing.key');                      // null
Settings::get('missing.key', 'fallback');          // 'fallback'

// Explicit type override
Settings::set('items.count', '10', 'int');         // stored and retrieved as int

// Check existence
Settings::has('app.name');                         // true

// Remove
Settings::forget('app.name');

// Get all settings
Settings::all();                                   // Collection<string, mixed>

// Get all settings in a group (keys prefixed with 'mail.')
Settings::all('mail');

// Remove everything
Settings::flush();

Settings::set('login.count', 0);

Settings::increment('login.count');          // 1
Settings::increment('login.count', 5);       // 6
Settings::decrement('login.count', 2);       // 4

// Works with floats
Settings::set('balance', 10.0);
Settings::increment('balance', 1.5);         // 11.5

// Creates the key if it doesn't exist
Settings::increment('new.counter');           // 1
Settings::decrement('new.gauge');             // -1

// Set multiple values at once
Settings::setMany([
    'app.name'   => 'My Portal',
    'app.locale' => 'en',
    'app.debug'  => false,
]);

// Get multiple values at once
$values = Settings::getMany(['app.name', 'app.locale', 'app.debug']);
// ['app.name' => 'My Portal', 'app.locale' => 'en', 'app.debug' => false]

Settings::set('enabled', true);
Settings::get('enabled'); // (bool) true

Settings::set('rate', 0.19);
Settings::get('rate'); // (float) 0.19

Settings::set('tags', ['php', 'laravel']);
Settings::get('tags'); // (array) ['php', 'laravel']

// config/settings.php
'defaults' => [
    'app.timezone' => 'UTC',
],

// Returns 'UTC' even if nothing is stored in the DB
Settings::get('app.timezone', 'Europe/London');

Settings::set('mail.host', 'smtp.example.com');
Settings::set('mail.port', 587);
Settings::set('app.name', 'My App');

Settings::all('mail');
// Collection {
//   'mail.host' => 'smtp.example.com',
//   'mail.port' => 587,
// }

Settings::setForUser($userId, 'theme', 'dark');
Settings::getForUser($userId, 'theme');         // 'dark'
Settings::hasForUser($userId, 'theme');         // true
Settings::forgetForUser($userId, 'theme');
Settings::allForUser($userId);
Settings::allForUser($userId, 'mail');          // filtered by group
Settings::flushForUser($userId);

'cache' => [
    'enabled' => false,
],
bash
php artisan vendor:publish --tag=settings-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=settings-config