PHP code example of ottosmops / settings

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

    

ottosmops / settings example snippets


use Ottosmops\Settings\Setting;

// Create a simple string setting
Setting::create([
    'key' => 'app_name',
    'value' => 'My Application',
    'type' => 'string',
    'description' => 'Application name'
]);

// Create with validation rules
Setting::create([
    'key' => 'max_users',
    'type' => 'integer',
    'rules' => '

// Set a value (with validation)
Setting::setValue('max_users', 50);

// Set without validation
Setting::setValue('max_users', 50, false);

// The value will be automatically cast to the defined type
Setting::setValue('is_active', 'true'); // Becomes boolean true
Setting::setValue('config_array', '["item1", "item2"]'); // Becomes array

// Using the static method
$appName = Setting::getValue('app_name');
$maxUsers = Setting::getValue('max_users', 100); // with default

// Using helper function (recommended for views)
$appName = setting('app_name');
$maxUsers = setting('max_users', 100); // with default

// Get as string representation
$configAsString = settingAsString('config_array');

Setting::create([
    'key' => 'email',
    'type' => 'string',
    'rules' => '-email');

// Create settings with scopes
Setting::create(['key' => 'bg_color', 'scope' => 'theme', ...]);
Setting::create(['key' => 'font_size', 'scope' => 'theme', ...]);

// Filter by scope in your application
$themeSettings = Setting::where('scope', 'theme')->get();

// Check if a setting exists
if (Setting::has('app_name')) {
    // Setting exists
}

// Check if a setting has a value
if (Setting::hasValue('app_name')) {
    // Setting exists and has a value
}

// Check if a setting is editable
if (Setting::isEditable('app_name')) {
    // Setting can be modified
}

Setting::remove('old_setting');

// This throws NoKeyIsFound if 'missing_key' doesn't exist
Setting::getValue('missing_key');

// This returns null if 'missing_key' doesn't exist
setting('missing_key');
bash
php artisan vendor:publish --tag=settings
bash
php artisan migrate
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Ottosmops\Settings\SettingsServiceProvider" --tag="settings-config" --force
bash
php artisan settings:flush-cache
# or
php artisan cache:clear
bash
php artisan settings:list

# Filter by scope
php artisan settings:list --scope=theme