PHP code example of hatchyu / laravel-settings

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

    

hatchyu / laravel-settings example snippets


'providers' => [
    Hatchyu\Settings\SettingsServiceProvider::class,
],

use Hatchyu\Settings\Facades\Settings;

// Set a global setting
settings()->set('app.name', 'My Super App');

// Get a global setting
$name = settings()->get('app.name'); // 'My Super App'

// Get with default value
$theme = settings()->get('ui.theme', 'light');

settings()->set('ui.theme', 'dark');
settings()->set('ui.language', 'en');

$ui = settings()->get('ui'); 
// Returns: ['ui.theme' => 'dark', 'ui.language' => 'en']

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Hatchyu\Settings\Contracts\SettingsScope;
use Hatchyu\Settings\Traits\HasSettings;

class User extends Authenticatable implements SettingsScope
{
    use HasSettings;

    /**
     * Define the fallback hierarchy for this model.
     * When a setting isn't found for the user, it checks these scopes in order.
     */
    public function getSettingsFallbackScopes(): array
    {
        return [
            $this->team,    // Check team settings first
            $this->company, // Then check company settings
        ];
    }
}

$user = User::find(1);

// Set a setting specifically for this user
settings()->scope($user)->set('ui.theme', 'dark');

// Retrieve it (it will look at User -> Team -> Company -> Global)
$theme = settings()->scope($user)->get('ui.theme');

// You can also use the trait method directly on the model
$user->settings()->set('notifications.email', true);
$user->settings()->get('notifications.email');
bash
php artisan migrate