PHP code example of sewidan / laravel-setting

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

    

sewidan / laravel-setting example snippets


# config/setting.php
return [
    'app_name' => 'My Application',
    'user_limit' => 10,
];

Setting::get('app_name');
//retruns
'My Application'

# config/setting.php
return [
    'priorities' => [
        'low' => 1,
        'medium' => 2,
        'hight' => 3
    ],
];

Setting::get('priorities.medium');
//retruns
2

# config/setting.php
return [
    'app_name' => [
        'type' => 'text', /* Optional config values */
        'max' => 255, /* Optional config values */
        'default_value' => 'My Application' /* <- This value will be returned by Setting::get('app_name') if key is not found in DB */
    ],
    'user_limit' => 10,
];

Setting::get('app_name');
//retruns
'My Application'

// You can still access the optional parameters
Setting::get('app_name.max');
//retruns
255

Setting::get('app_name.');
//retruns
[
    'type' => 'text',
    'max' => 255,
    'default_value' => 'My Application',
    'value' => 'My Custom Application Name' //the value key will be added with the current value saved in the database (or default if not in database yet)
]

# config/setting.php
return [
    'user_*' => [
        'dark_mode' => false,
        'permissions' => [
            'read' => true,
            'write' => false,
        ]
    ],
];

// Save a new setting under user_1.dark_mode with a value of true
Setting::set("user_{$user->id}.dark_mode", true);

Setting::get("user_{$user->id}.dark_mode");
//returns
true

Setting::get("user_{$otherUser->id}.dark_mode");
//returns
false

Setting::set("user_{$otherUser->id}.dark_mode", true);
Setting::set("user_{$otherUser->id}.permissions.write", true);

Setting::get("user_{$otherUser->id}");
//returns
[
    'dark_mode' => true,
    'permissions' => [
        'write' => false
    ]
]

Setting::get("user_{$otherUser->id}.");
// same as
Setting::getWithDefaultSubKeys("user_{$otherUser->id}");

//returns
[
    'dark_mode' => true, // this value comes from the database
    'permissions' => [
        'read' => true, // this value is the default from the config
        'write' => false, // this value is the default from the config
    ]
]

Setting::get('name');
// get setting value with key 'name'
// If this key is not found in DB then it will return the value defined from the config file or null if the key is also not defined in the config file.

Setting::get('name', 'Joe');
// get setting value with key 'name'
// return 'Joe' if the key does not exists. This will overwrite the default coming from the config file.

Setting::all();
// get all settings.
// This will merge the setting.php config file with the values (only where lang is null) found in the database and returns a collection.

Setting::lang('zh-TW')->get('name', 'Joe');
// get setting value with key and language

Setting::set('name', 'Joe');
// set setting value by key

Setting::lang('zh-TW')->set('name', 'Joe');
// set setting value by key and language

Setting::has('name');
// check the key exists in database, return boolean

Setting::lang('zh-TW')->has('name');
// check the key exists by language in database, return boolean

Setting::forget('name');
// delete the setting from database by key

Setting::lang('zh-TW')->forget('name');
// delete the setting from database by key and language

Setting::lang(App::getLocale())->langResetting(false);

'model' => \App\YourModelName::class,
bash
php artisan vendor:publish --tag=setting
php artisan migrate