PHP code example of sgflores / schema-settings

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

    

sgflores / schema-settings example snippets




namespace App\Providers\SchemaSettings;

use SgFlores\SchemaSetting\Contracts\ConfigurableInterface;
use SgFlores\SchemaSetting\Items\ConfigurableItem;

class GlobalSettings implements ConfigurableInterface
{
    public static function getKey(): ?string
    {
        return null; // Null indicates global scope
    }

    public static function registerConfigurables(): array
    {
        return [
            // Basic Settings
            ConfigurableItem::make('site_name')
                ->type(ConfigurableItem::TYPE_STRING)
                ->default('Awesome App')
                ->group('general')
                ->label('Site Name')
                ->description('The name of your application')
                ->rules(['
        ];
    }
}



return [
    App\Providers\AppServiceProvider::class,
    App\Providers\SchemaSettings\SchemaSettingServiceProvider::class, // Add this line
];



namespace App\Providers\SchemaSettings;

use Illuminate\Support\ServiceProvider;
use SgFlores\SchemaSetting\Facades\Settings;

class SchemaSettingServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Register your schema classes here
        Settings::register(GlobalSettings::class);
        Settings::register(UserSettings::class);
    }
}

use SgFlores\SchemaSetting\Facades\Settings;
use App\Providers\SchemaSettings\GlobalSettings;

public function boot(): void
{
    Settings::register(GlobalSettings::class);
}

use SgFlores\SchemaSetting\Facades\Settings;

// Get a setting
$siteName = Settings::get('site_name');

// Set a setting
Settings::set('site_name', 'My Awesome App');

// Get multiple settings
$settings = Settings::getMultiple(['site_name', 'maintenance_mode']);

// Get all settings
$all = Settings::all();

// Get schema with values for form generation
$schemaWithValues = Settings::getSchemaWithValues(['site_name', 'site_description']);
// Returns array with schema configuration + 'value' property for each setting

// Get a setting
$siteName = setting('site_name');

// Set a setting
set_setting('site_name', 'My Awesome App');

// Check if setting exists
if (has_setting('site_name')) {
    // ...
}

// Get schema with values for form generation
$formSchema = schema_with_values(['site_name', 'site_description']);

// app/Providers/SchemaSettings/UserSettings.php
namespace App\Providers\SchemaSettings;

use SgFlores\SchemaSetting\Contracts\ConfigurableInterface;
use SgFlores\SchemaSetting\Items\ConfigurableItem;

class UserSettings implements ConfigurableInterface
{
    public static function getKey(): ?string
    {
        return \App\Models\User::class;
    }

    public static function registerConfigurables(): array
    {
        return [
            // Notification Settings
            ConfigurableItem::make('email_notifications')
                ->type(ConfigurableItem::TYPE_BOOLEAN)
                ->default(true)
                ->group('notifications')
                ->label('Email Notifications')
                ->description('Receive notifications via email'),

            ConfigurableItem::make('timezone')
                ->type(ConfigurableItem::TYPE_STRING)
                ->default('UTC')
                ->group('localization')
                ->label('Timezone')
                ->description('Your timezone for date/time display')
                ->rules(['

use SgFlores\SchemaSetting\Traits\HasSettings;

class User extends Model
{
    use HasSettings;
}

// Get user-specific setting
$notifications = $user->setting('email_notifications');

// Set user-specific setting
$user->setSetting('timezone', 'America/New_York');

ConfigurableItem::make('default_role')
    ->type(ConfigurableItem::TYPE_STRING)
    ->default('user')
    ->lazyOptions(function() {
        return Role::pluck('name')->toArray();
    });

// ❌ This will throw InvalidSchemaException immediately
ConfigurableItem::make('setting')
    ->type(ConfigurableItem::TYPE_INTEGER)
    ->default('hello'); // Type mismatch!

// ✅ This works correctly
ConfigurableItem::make('setting')
    ->type(ConfigurableItem::TYPE_STRING)
    ->default('hello');

'routes' => [
    'prefix' => env('SCHEMA_SETTINGS_ROUTE_PREFIX', 'api/schema-settings'),
    'middleware' => env('SCHEMA_SETTINGS_MIDDLEWARE', null),
    'name_prefix' => 'schema_settings.',
    'enabled' => env('SCHEMA_SETTINGS_ROUTES_ENABLED', true),
],

return [
    'table_name' => 'schema_settings',
    
    'cache' => [
        'enabled' => true,
        'store' => null,
        'prefix' => 'schema_settings_',
        'ttl' => 3600,
    ],
    
    'audit' => [
        'enabled' => true,
        'table_name' => 'schema_settings_history',
    ],

    'validation' => [
        'strict_mode' => true,        // Validate during fluent chain
        'boot_validation' => true,    // Validate during registration  
        'enhanced_errors' => true,    // Provide detailed error messages
    ],

    'routes' => [
        'prefix' => env('SCHEMA_SETTINGS_ROUTE_PREFIX', 'api/schema-settings'),
        'middleware' => env('SCHEMA_SETTINGS_MIDDLEWARE', null),
        'name_prefix' => 'schema_settings.',
        'enabled' => env('SCHEMA_SETTINGS_ROUTES_ENABLED', true),
    ],
];
bash
# Publish config file
php artisan vendor:publish --tag=schema-settings-config

# Publish migrations (optional)
php artisan vendor:publish --tag=schema-settings-migrations

# Run migrations
php artisan migrate
bash
php artisan vendor:publish --tag=schema-settings-configurables