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');
// ❌ 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');