PHP code example of elegantly / laravel-settings

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

    

elegantly / laravel-settings example snippets


use Elegantly\Settings\Models\Setting;

return [

    /*
     * The Eloquent model used to store and retrieve settings
     */
    'model' => Setting::class,

    /*
     * Cache configuration for global settings
     */
    'cache' => [
        'enabled' => true,
        'key' => 'settings',
        'ttl' => 60 * 60 * 24, // 1 day
    ],

];

use Elegantly\Settings\Facades\Settings;

// Set a value
Settings::set(
    namespace: 'home',
    name: 'color',
    value: 'white'
);

// Get a value
$setting = Settings::get(
    namespace: 'home',
    name: 'color'
);

$setting->value; // white

namespace App\Http\Controllers;

use Elegantly\Settings\Settings;

class UserController extends Controller
{
    public function index(Settings $settings)
    {
        $settings->set(
            namespace: 'home',
            name: 'color',
            value: 'white'
        );

        $setting = $settings->get(
            namespace: 'home',
            name: 'color'
        );

        $setting->value; // white
    }
}

namespace App\Http\Controllers;

use App\Settings\HomeSettings;

class UserController extends Controller
{
    public function index(HomeSettings $settings)
    {
        $settings->color; // white

        $settings->color = 'black';
        $settings->save();
    }
}

namespace App\Settings;

use Elegantly\Settings\NamespacedSettings;

class HomeSettings extends NamespacedSettings
{
    public ?string $color = null;

    /** @var int[] */
    public array $articles = [];

    public static function getNamespace(): string
    {
        return 'home';
    }
}
bash
php artisan vendor:publish --tag="settings-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="settings-config"