PHP code example of glorand / laravel-model-settings

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

    

glorand / laravel-model-settings example snippets


public $settingsFieldName = 'user_settings';

use Glorand\Model\Settings\Traits\HasSettingsField;

class User extends Model
{
    use HasSettingsField;

    //define only if you select a different name from the default
    public $settingsFieldName = 'user_settings';

    //define only if the model overrides the default connection
    protected $connection = 'mysql';

}

use Glorand\Model\Settings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;
}

use Glorand\Model\Settings\Traits\HasSettingsRedis;

class User extends Model
{
    use HasSettingsRedis;
}

return [
    // start other config options

    // end other config options

    // defaultConfigs
    'defaultSettings' => [
        'users' => [
            'key_1' => 'val_1',
        ]
    ]
];

use Glorand\Model\Settings\Traits\HasSettingsTable;

class User extends Model
{
    public $defaultSettings = [
        'key_1' => 'val_1',
    ];
}

$user = App\User::first();

$user->settings()->empty();

$user->settings()->exist();

$user->settings()->all();
$user->settings()->get();

$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
//multiple
$user->settings()->getMultiple(
	[
		'some.setting_1',
		'some.setting_2',
	],
	'default value'
);

$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
//multiple
$user->settings()->setMultiple([
	'some.setting_1' => 'new value 1',
	'some.setting_2' => 'new value 2',
]);

$user->settings()->has('some.setting');

$user->settings()->delete('some.setting');
//multiple
$user->settings()->deleteMultiple([
	'some.setting_1',
	'some.setting_2',
]);
//all
$user->settings()->clear();

protected $persistSettings = true; //boolean

'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', true),

class User extends Model
{
    use HasSettingsTable;

    public array $defaultSettings = [
        'user' => [
            'name' => 'Test User',
            'email' => '[email protected]'
            'age' => 27,
        ],
        'language' => 'en',
        'max_size' => 12,
    ];

    // settings rules
    public array $settingsRules = [
        'user' => 'array',
        'user.email' => [
            'string',
            'email',
        ],
        'user.age' => 'integer',
        'language' => 'string|in:en,es,it|max:2',
        'max_size' => 'int|min:5|max:15',
    ];
}