PHP code example of michaelnabil230 / laravel-setting

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

    

michaelnabil230 / laravel-setting example snippets


use MichaelNabil230\Setting\Models\Setting;

return [
    /*
    |--------------------------------------------------------------------------
    | Default Settings Store
    |--------------------------------------------------------------------------
    |
    | This option controls the default settings store that gets used while
    | using this settings library.
    |
    | Supported: "json", "database", "redis"
    |
    */

    'default' => 'json',

    /*
    |--------------------------------------------------------------------------
    | Drivers Stores
    |--------------------------------------------------------------------------
    |
    | The settings are stored.
    |
    */

    'drivers' => [
        'database' => [
            'driver' => \MichaelNabil230\Setting\Stores\DatabaseSettingStore::class,
            'options' => [
                'model' => Setting::class,
                'table' => 'settings', // name of table in dataBase
                'cache' => [
                    'enableCache' => false,
                    'cacheTtl' => 15, // TTL in seconds.
                ]
            ],
        ],

        'redis' => [
            'driver' => \MichaelNabil230\Setting\Stores\RedisSettingStore::class,
            'options' => [
                'connection' => 'default',
                'prefix' => 'setting',
            ],
        ],

        'json' => [
            'driver' => \MichaelNabil230\Setting\Stores\JsonSettingStore::class,
            'options' => [
                'path' => storage_path('settings.json'),
            ]
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Keys
    |--------------------------------------------------------------------------
    |
    | Your keys are used to insert settings data.
    |
    */

    'keys' => [
        // 
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Settings
    |--------------------------------------------------------------------------
    |
    | Default settings are used when a setting is not found in the store.
    |
    */

    'defaults' => [
        // 
    ],
];



use MichaelNabil230\Setting\Facades\Setting;

Setting::set('foo', 'bar')->save();
Setting::get('foo', 'default value');
Setting::get('nested.element');
Setting::has('foo');
Setting::flip('foo');
Setting::enable('foo');
Setting::disable('foo');
Setting::forget('foo');
Setting::forgetAll();

$settings = Setting::all();


// Get the store instance
setting();

// Get values
setting('foo');
setting('foo.bar');
setting('foo', 'default value');
setting()->get('foo');
setting()->get('foo.bar');

// Set values
setting(['foo' => 'bar'])->save();
setting(['foo.bar' => 'baz'])->save();
setting()->set('foo', 'bar')->save();

// Flipping a boolean setting:

setting()->set('notifications', true)->save();

// Disable notifications.
setting()->flip('notifications')->save();

dd(setting()->get('notifications')); // Returns false.

// Enable notifications.
setting()->flip('notifications')->save();

dd(setting()->get('notifications')); // Returns true.

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

dd(setting()->get('new-key')); // Returns true.

// Enabling a boolean setting:

setting()->set('notifications', false)->save();

setting()->enable('notifications')->save();

dd(setting()->get('notifications')); // Returns true.

// Disabling a boolean setting:

setting()->set('notifications', true)->save();

setting()->disable('notifications')->save();

dd(setting()->get('notifications')); // Returns false.

// Method chaining
setting(['foo' => 'bar'])->save();

'cache' => [
  'enableCache' => false,
  'cacheTtl' => 15, // TTL in seconds.
]



return [
    /*
    |--------------------------------------------------------------------------
    | Default Settings Store
    |--------------------------------------------------------------------------
    |
    | This option controls the default settings store that gets used while
    | using this settings library.
    |
    | Supported: "json", "database", "redis"
    |
    */
    'default' => 'json',

    // ...
];



return [
    //...
    
    /*
    |--------------------------------------------------------------------------
    | Drivers Stores
    |--------------------------------------------------------------------------
    |
    | The settings are stored.
    |
    */
    
    'drivers' => [
        'database' => [
            'driver' => \MichaelNabil230\Setting\Stores\DatabaseSettingStore::class,
            'options' => [
                'model' => Setting::class,
                'table' => 'settings', // name of table in dataBase
                'cache' => [
                    'enableCache' => false,
                    'cacheTtl' => 15, // TTL in seconds.
                ]
            ],
        ],

        'redis' => [
            'driver' => \MichaelNabil230\Setting\Stores\RedisSettingStore::class,
            'options' => [
                'connection' => 'default',
                'prefix' => 'setting',
            ],
        ],

        'json' => [
            'driver' => \MichaelNabil230\Setting\Stores\JsonSettingStore::class,
            'options' => [
                'path' => storage_path('settings.json'),
            ]
        ],
    ],
];

 

// ...
'custom' => [
    'driver'  => App\Stores\CustomStore::class,
    
    'options' => [
        // ...
    ],
],

 

namespace App\Settings;

use MichaelNabil230\Setting\Stores\SettingStore as Store;

class CustomStore implements Store
{
    // Implement the contract's methods here
} 

return [
    'drivers' => [
        'custom' => [
            'driver'  => App\Settings\CustomStore::class,
        ],
    ],
];

return [
    'drivers' => [
        'custom' => [
            'driver'  => App\Settings\CustomStore::class,
            'options' => [
                // more customize
            ],
        ],
    ],
];

Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
    \MichaelNabil230\Setting\Stores\DatabaseSettingStore::$cacheKey = 'setting.cache.tenant.' . $event->tenancy->tenant->id;
});
bash
php artisan vendor:publish --tag="setting-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="setting-config"

php artisan setting:forget foo
php artisan setting:get || php artisan setting:get foo
php artisan setting:set-or-update foo bar

php artisan vendor:publish --tag="setting-migrations"
mv database/migrations/*_create_settings_table.php database/migrations/tenant