PHP code example of rudnev / laravel-settings

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

    

rudnev / laravel-settings example snippets


// app settings:

settings()->set('timezone', 'UTC');
settings()->get('timezone');

// the same:

settings(['timezone' => 'UTC']);
settings('timezone');

// user settings:

settings()->scope($user)->set('lang', 'en');
settings()->scope($user)->get('lang');


Settings::set('timezone', 'UTC');
Settings::get('timezone');



namespace App\Http\Controllers;

use Rudnev\Settings\Contracts\RepositoryContract as Settings;

class MyController extends Controller
{
    public function index(Settings $settings)
    {
        $tz = $settings->get('timezone');
    }
}

Settings::has('foo');

Settings::get('foo');

// You can specify a default value when an property is null or not found:

Settings::get('foo', 'default'); 

Settings::get(['foo', 'bar', 'baz']);

// with default value:

Settings::get(['foo', 'bar', 'baz' => 'default']);

Settings::all();

Settings::set('foo', 'bar');

Settings::set([
    'foo' => 'bar', 
    'qux' => 'baz'
]);

Settings::forget('foo');

Settings::forget(['foo', 'bar']);

Settings::flush();

Settings::set('products.desk', ['price' => 300, 'height' => 100]);
Settings::forget('products.desk.price');
Settings::has('products.desk.height');
Settings::get('products.desk');

$user = auth()->user();

Settings::scope($user)->set('lang', 'en');

Settings::scope($user)->get('lang');

use Rudnev\Settings\Traits\HasSettings;
use Illuminate\Database\Eloquent\Model;

class User extends Model 
{
    use HasSettings;
    
    // ...
}

$user = new User();
$user->settings = ['lang' => 'en'];
$user->save();

echo $user->settings['lang'];

$user->settings['timezone'] = 'UTC';
$user->save();

$user->settings()->set('lang', 'en');
$user->settings()->get('lang');

// the same:

$user->settings(['lang' => 'en']);
$user->settings('lang');

use Rudnev\Settings\Traits\HasSettings;
use Illuminate\Database\Eloquent\Model;

class User extends Model 
{
    use HasSettings;
    
    protected $settingsConfig = [
        'default' => [
            'timezone' => 'UTC'
        ]
    ];
    
    // ...
}

protected $settingsConfig = [
    'store' => 'database',
	
    'default' => [
        'timezone' => 'UTC'
    ]
];

// Set
Settings::scope('my-landing-page')->set('tracking_id', 'UA-000000-2');

// Get
Settings::scope('my-landing-page')->get('tracking_id');



return [
    'user' => [
        'language' => [
            'name' => 'Language',
            'description' => 'The site language.'    
        ],
        
        'mfa' => [
            'name' => 'MFA',
            'description' => 'Multi-factor authentication.'    
        ],
    ]
];

$user = auth()->user();
$userSettings = Settings::scope($user);

// Store locale:
$userSettings->set('language', 'en');

// Store some settings:
$userSettings->set('mfa', 'enabled');

// Retrieve locale:
$locale = $userSettings->get('language');

// Apply locale:
App::setLocale($locale);

// Print translated strings:
foreach ($userSettings->all() as $key => $value)
{
    $name = trans("settings.user.$key.name");
    $desc = trans("settings.user.$key.description");
    
    echo "$name: $value ($desc)" . PHP_EOL;
}

use Rudnev\Settings\Contracts\StoreContract;

class MongoStore implements StoreContract {
// ...
}

Settings::extend('mongo', function($app, $storeName, $config)
{
    return Settings::repository(new MongoStore);
});

Settings::store('mongo')->get('foo');
bash
php artisan vendor:publish --provider="Rudnev\Settings\ServiceProvider"
bash
php artisan migrate
bash
php artisan settings:clear-cache