PHP code example of x-laravel / eloquent-settings

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

    

x-laravel / eloquent-settings example snippets


Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->settingsBag(); // adds a nullable JSON settings column
    $table->timestamps();
});

use Illuminate\Database\Eloquent\Model;
use XLaravel\EloquentSettings\HasSettingsBag;

class User extends Model
{
    use HasSettingsBag;
}

$user->settings()->all(); // ['theme' => 'dark', 'lang' => 'tr']

$user->settings()->get('theme');           // 'dark'
$user->settings()->get('theme', 'light'); // default value if missing
$user->settings()->get('layout.boxed');   // dot notation for nested keys

$user->settings()->has('theme');        // true / false
$user->settings()->has('layout.boxed'); // dot notation supported

$user->settings()->update('theme', 'dark');
$user->settings()->update('layout.boxed', true); // dot notation for nested keys

$user->settings()->delete('theme');        // removes one setting
$user->settings()->delete('layout.boxed'); // dot notation supported
$user->settings()->delete();              // clears all settings

$user->settings()->apply(['theme' => 'dark', 'lang' => 'tr']);

$user->settings()
    ->update('theme', 'dark')
    ->update('lang', 'tr');

Schema::create('user_theme_settings', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->settingsBag();
    $table->timestamps();
});

use Illuminate\Database\Eloquent\Model;
use XLaravel\EloquentSettings\HasSettingsBag;

class UserThemeSetting extends Model
{
    use HasSettingsBag;
}

class User extends Model
{
    use HasSettingsBag;

    public function themeSettings()
    {
        return $this->hasOne(UserThemeSetting::class);
    }
}

$user->settings('theme')->get('layout.boxed');
$user->settings('theme')->update('color', 'blue');
$user->settings('theme')->delete('color');

class User extends Model
{
    use HasSettingsBag;

    protected $defaultSettings = [
        'theme' => 'light',
        'notifications' => true,
    ];
}

$user = User::create([]); // settings initialised from $defaultSettings automatically
$user->settings()->get('theme'); // 'light'

class User extends Model
{
    use HasSettingsBag;

    protected $allowedSettings = ['theme', 'language'];
}

$user->settings()->apply(['theme' => 'dark', 'language' => 'tr', 'other' => 'ignored']);
$user->settings()->all(); // ['theme' => 'dark', 'language' => 'tr']

class User extends Model
{
    use HasSettingsBag;

    protected $mapSettingsTo = 'config';
}

$user->config()->get('theme');
$user->config('theme')->update('color', 'blue');