PHP code example of synergitech / laravel-multiconfig

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

    

synergitech / laravel-multiconfig example snippets


User::find(1)->settings->set('color', 'red');

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use SynergiTech\Multiconfig\HasConfig;

class User extends Authenticatable
{
    use HasConfig;

    // ...
}

use SynergiTech\Multiconfig\Facades\Setting;

Setting::name('color')->string();

use SynergiTech\Multiconfig\Facades\Setting;

Setting::name('dark_mode')->boolean();

use SynergiTech\Multiconfig\Facades\Setting;

Setting::name('color')->string()->default('black');

Setting::name('color')->disabled();

Setting::name('color')->group('theme');

Setting::name('color')->group('theme')->bag('style');

Setting::name('notify_email')->boolean()->default(true)->bag('notifications');
Setting::name('notify_sms')->boolean()->default(false)->bag('notifications');

use SynergiTech\Multiconfig\Facades\Setting;

Setting::name('color')->string()->default('black');

// This new setting will be created
Setting::name('notifications')->boolean()->default(true);

use SynergiTech\Multiconfig\Facades\Setting;

// Commenting this line will remove the "color" setting on migration.
// Setting::name('color')->string()->default('black');

// This new setting will be created
Setting::name('notifications')->boolean()->default(true);

Setting::name('color')->string()->bag('theme');

// This is the new declaration.
// Setting::name('color')
//    ->array()
//    ->default(['black'])
//    ->group('theme');

// This is the old declaration.
// Setting::name('color')->string()->bag('theme');

Setting::name('color')
    ->array()
    ->default(['black'])
    ->group('theme');

Setting::name('color')
    ->array()
    ->default('black')
    ->group('theme')
    ->using(fn ($old) => $old->value ?? 'black'); // If the value is null, set it as "black".

// This old declaration will be deleted after the migration ends.
// Setting::name('color')->string()->bag('theme');

// This is a new setting.
Setting::name('dark')
    ->boolean()
    ->default(false)
    ->group('theme')
    ->from('color')
    ->using(static fn ($old) => $old->value === 'black'); // If it's black, then it's dark.

$user = User::find(1);

echo "Your color is: {$user->settings->get('color')}.";

// app/Models/User.php

/**
 * Check if the user should initialize settings automatically after creation.
 *
 * @return bool
 */
protected function shouldInitializeConfig(): bool
{
    // Don't initialize the settings if the user is not verified from the start.
    // We will initialize them only once the email is properly verified.
    return null !== $this->email_verified_at;
}

// Initialize if not initialized before.
$user->settings()->initialize();

// Forcefully initialize, even if already initialized.
$user->settings()->initialize(true);

if ($user->settings()->isInitialized()) {
    return 'You have a config!';
}

return "Your favorite color is {$user->settings->color}";

return "Your favorite color is {$user->settings->value('color')}";

$setting = $user->settings->get('theme');

echo "You're using the [$setting->value] theme.";

foreach ($user->settings as $setting) {
    echo "The [$setting->name] has the [$setting->value] value.";
}

$user->settings->only('colors', 'is_dark');

$user->settings->except('dark_mode');

$user->settings->groups(); // or ->groupBy('group')

$user->settings->color = 'red';

$user->settings->set('color-default', 'red');

$setting = $user->settings->get('color');

$setting->value = 'red';
$setting->save();

$user->settings->set([
    'color' => 'red',
    'dark_mode' => false,
]);

$setting = $user->settings->get('color');

$setting->setDefault();

$user->settings->setDefault('color');

if ($user->settings->isNull('color')) {
    return 'The color setting is not set.';
}

$user->settings->enable('color');

$user->settings->disable('color');

> $user->settings->setIfEnabled('color', 'red');
> 

// app/Models/User.php
i

use SynergiTech\Multiconfig\Eloquent\Scopes\FilterBags;

$allSettings = $user->settings()->withoutGlobalScope(FilterBags::class)->get();

/**
 * Returns the bags this model uses for settings.
 *
 * @return array|string
 */
public function filterBags(): array|string|null
{
    return null;
}

$user->settings->regenerate();

$user->settings->invalidate();

$user->settings->regeneratesOnExit = true;

use App\Models\User;
use Illuminate\Http\Request;

public function store(Request $request, User $user)
{
    $settings = $request->validate([
        'age' => '

public function test_user_has_settings(): void
{
    Metadata::forceCreate([
        'name'    => 'foo',
        'type'    => 'string',
        'default' => 'bar',
        'bag'     => 'users',
        'group'   => 'default',
    ]);

    $user = User::create([
        // ...
    ]);

    // ...
}