PHP code example of codersandip / laravel-dynamic-settings

1. Go to this page and download the library: Download codersandip/laravel-dynamic-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/ */

    

codersandip / laravel-dynamic-settings example snippets


return [
    // Cache driver (file, redis, memcached, array)
    'cache_driver' => env('SETTINGS_CACHE_DRIVER', 'file'),

    // Cache TTL in seconds (3600 = 1 hour)
    'cache_ttl' => env('SETTINGS_CACHE_TTL', 3600),

    // Cache key prefix
    'cache_prefix' => env('SETTINGS_CACHE_PREFIX', 'dyn_setting'),

    // Admin panel URL prefix
    'route_prefix' => env('SETTINGS_ROUTE_PREFIX', 'admin/settings'),

    // Middleware applied to admin routes
    'route_middleware' => ['web', 'auth'],

    // Available groups in the dropdown
    'groups' => ['general', 'email', 'payment', 'security'],
];

// Get a setting
setting('site_name');                     // → 'My Application'

// Get with a default value
setting('site_name', 'Default App');      // → 'Default App' if not set

// Get with tenant context (falls back to global if not found)
setting('currency', 'USD', $tenantId);

// Get with tenant + user context
setting('theme', 'light', $tenantId, $userId);

// Via helper (chained)
setting()->set('site_name', 'My App');

// With options
setting()->set('page_size', 20, [
    'type'        => 'integer',
    'group'       => 'general',
    'description' => 'Results per page',
]);

use Codersandip\DynamicSettings\Facades\Settings;

Settings::get('site_name');
Settings::get('site_name', 'Default');
Settings::set('site_name', 'New Name');
Settings::delete('site_name');
Settings::has('site_name');

use Codersandip\DynamicSettings\Services\SettingsService;

class MyController extends Controller
{
    public function __construct(private SettingsService $settings) {}

    public function index()
    {
        $siteName = $this->settings->get('site_name', 'My App');
        // ...
    }
}

setting()->set('enabled',  true,             ['type' => 'boolean']);
setting()->set('count',    100,              ['type' => 'integer']);
setting()->set('gateways', ['stripe', 'pp'], ['type' => 'json']);

setting('enabled');   // → true  (bool)
setting('count');     // → 100   (int)
setting('gateways');  // → ['stripe', 'pp']  (array)

// Store global setting
setting()->set('currency', 'USD');

// Store tenant-specific override
setting()->set('currency', 'GBP', ['tenant_id' => 5]);

// Store user-specific override (within a tenant)
setting()->set('currency', 'EUR', ['tenant_id' => 5, 'user_id' => 12]);

// Retrieval
setting('currency');              // → 'USD'  (global)
setting('currency', null, 5);    // → 'GBP'  (tenant 5 override)
setting('currency', null, 5, 12);// → 'EUR'  (user 12 override)
setting('currency', null, 5, 99);// → 'GBP'  (user 99 falls back to tenant)
setting('currency', null, 9);    // → 'USD'  (tenant 9 falls back to global)

use Codersandip\DynamicSettings\Facades\Settings;

// Set tenant context once
$tenantSettings = Settings::forTenant(5);

$tenantSettings->get('currency');    // uses tenant 5
$tenantSettings->get('locale');

// Chain tenant + user
Settings::forTenant(5)->forUser(12)->get('theme');

'route_middleware' => ['web', 'auth', 'can:manage-settings'],

// config/settings.php
'disable_routes' => true,
bash
php artisan migrate
bash
php artisan settings:seed
bash
php artisan settings:cache
php artisan settings:clear
php artisan settings:seed
php artisan settings:seed --force   # overwrite existing settings
bash
php artisan vendor:publish --tag=settings-routes