PHP code example of zachleigh / laravel-property-bag

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

    

zachleigh / laravel-property-bag example snippets


$app->register(LaravelPropertyBag\ServiceProvider::class);

...
use LaravelPropertyBag\Settings\HasSettings;

class User extends Model
{
    use HasSettings;

    ...
}

protected $registeredSettings = [
    'example_setting' => [
        'allowed' => [true, false],
        'default' => false
    ]
];

$user->settings(['example_setting' => false]);
// or
$user->settings()->set(['example_setting' => false]);
// or
$user->setSettings(['example_setting' => false]);

$user->settings([
    'example_setting' => false,
    'another_setting' => 'grey'
]);

$value = $user->settings('example_setting');
// or
$value = $user->settings()->get('example_setting');

$value = $model->settings()->get($key);

$model->settings()->set([
  'key1' => 'value1',
  'key2' => 'value2'
]);

// or

$model->setSettings([
  'key1' => 'value1',
  'key2' => 'value2'
]);

$default = $model->settings()->getDefault($key);

// or

$default = $model->defaultSetting($key);

$defaults = $model->settings()->allDefaults();

// or

$defaults = $model->defaultSetting();

$allowed = $model->settings()->getAllowed($key);

// or

$allowed = $model->allowedSetting($key);

$allowed = $model->settings()->allAllowed();

// or

$allowed = $model->allowedSetting();

$boolean = $model->settings()->isDefault($key, $value);

$boolean = $model->settings()->isValid($key, $value);

$allSettings = $model->settings()->all();

// or

$allSettings = $model->allSettings();

'integer' => [
    'allowed' => ':int:',
    'default' => 7
]

'range' => [
    'allowed' => ':range=1,5:',
    'default' => 1
]

'range' => [
    'allowed' => ':range=1,10:',
    'default' => 5
]

'setting_name' => [
    'allowed' => ':example:',
    'default' => 'default'
]

public static function ruleExample($value)
{
    // do stuff
    //
    // return boolean;
}

'setting_name' => [
    'allowed' => ':example=arg1,arg2:',
    'default' => 'default'
]

public static function ruleExample($value, $arg1, $arg2)
{
    // do stuff
    //
    // return boolean;
}

/**
 * Return a collection of registered settings.
 *
 * @return Collection
 */
public function registeredSettings()
{
    // Your code

    return $collection;
}

'example_setting' => [
    'allowed' => [true, false],
    'default' => true
]

/**
 * Return a collection of registered settings.
 *
 * @return Collection
 */
public function registeredSettings()
{
    $allGroups = Auth::user()->allGroupNames();

    return collect([
        'default_group' => [
            'allowed' => $allGroups,
            'default' => $allGroups[0]
        ]
    ]);
}

/**
 * Get array of all group names.
 *
 * @return array
 */
public function allgroupNames()
{
    return $this->groups->pluck('name')->all();
}

/**
 * Return a collection of registered settings.
 *
 * @return Collection
 */
public function registeredSettings()
{
    $allGroups = getResource()->allGroupNames();

    return collect([
        'default_group' => [
            'allowed' => $allGroups,
            'default' => $allGroups[0]
        ]
    ]);
}

php artisan vendor:publish --provider="LaravelPropertyBag\ServiceProvider"

php artisan migrate

php artisan pbag:make {resource}

php artisan pbag:make User

$boolean = $model->settings()->keyIs($key, $value);

$default = $model->settings()->reset($key);

php artisan pbag:rules