PHP code example of moirei / settings

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

    

moirei / settings example snippets


$notificationsEnabled = $user->settings->get('notifications.enable');

use MOIREI\Settings\AsSettingsCollection;
use MOIREI\Fields\Inputs\Boolean;
...
class User extends Model{
    ...
    protected $casts = [
        'settings' => AsSettingsCollection::class,
    ];

    /**
     * Get settings configuration (optional)
     *
     * @return array
     */
    public function settingsConfig(): array
    {
        return [
            'notifications.enable' => Boolean::make('Enable notification')->default(false),
        ];
    }
}

$user = new User();
$notificationsEnabled = $user->settings->get('notifications.enable');

// Or provide a prefered default
$notificationsEnabled = $user->settings->get('notifications.enable', true);

$notifications = $this->model->settings->notifications;

expect($notifications)->toBeArray();

use MOIREI\Settings\HasSettings;
...
class User extends Model{
    use HasSettings;
    ...
}

$user = new User();
expect($user->settings())->toBeCollection();
expect($user->settings('notifications.enable'))->toBeBool();

class UserSettings extends Settings
{
    /**
     * @inheritdoc
     */
    public function fields(): array
    {
        return [
            'notifications.enable' => Boolean::make('Enable notification')->default(false)
        ];
    }
}

// config/settings.php
'groups' => [
   'users' => \App\Settings\UserSettings::class,
],

class User extends Model{
    use HasSettings;
    
    // optional if lower-cased pluralised form of class name matches name in groups
    protected $settingsConfig = 'users';
}
bash
php artisan vendor:publish --tag="model-settings"