1. Go to this page and download the library: Download spatie/laravel-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/ */
spatie / laravel-settings example snippets
class GeneralSettings extends Settings
{
public string $site_name;
public bool $site_active;
public static function group(): string
{
return 'general';
}
}
class GeneralSettingsController
{
public function show(GeneralSettings $settings){
return view('settings.show', [
'site_name' => $settings->site_name,
'site_active' => $settings->site_active
]);
}
}
class GeneralSettingsController
{
public function update(
GeneralSettingsRequest $request,
GeneralSettings $settings
){
$settings->site_name = $request->input('site_name');
$settings->site_active = $request->input('site_active');
$settings->save();
return redirect()->back();
}
}
return [
/*
* Each settings class used in your application must be registered, you can
* add them (manually) here.
*/
'settings' => [
],
/*
* The path where the settings classes will be created.
*/
'setting_class_path' => app_path('Settings'),
/*
* In these directories settings migrations will be stored and ran when migrating. A settings
* migration created via the make:settings-migration command will be stored in the first path or
* a custom defined path when running the command.
*/
'migrations_paths' => [
database_path('settings'),
],
/*
* When no repository is set for a settings class, the following repository
* will be used for loading and saving settings.
*/
'default_repository' => 'database',
/*
* Settings will be stored and loaded from these repositories.
*/
'repositories' => [
'database' => [
'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class,
'model' => null,
'table' => null,
'connection' => null,
],
'redis' => [
'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class,
'connection' => null,
'prefix' => null,
],
],
/*
* The encoder and decoder will determine how settings are stored and
* retrieved in the database. By default, `json_encode` and `json_decode`
* are used.
*/
'encoder' => null,
'decoder' => null,
/*
* The contents of settings classes can be cached through your application,
* settings will be stored within a provided Laravel store and can have an
* additional prefix.
*/
'cache' => [
'enabled' => env('SETTINGS_CACHE_ENABLED', false),
'store' => null,
'prefix' => null,
],
/*
* These global casts will be automatically used whenever a property within
* your settings class isn't the default PHP type.
*/
'global_casts' => [
DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class,
DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class,
// Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class,
Spatie\LaravelData\Data::class => Spatie\LaravelSettings\SettingsCasts\DataCast::class,
],
/*
* The package will look for settings in these paths and automatically
* register them.
*/
'auto_discover_settings' => [
app_path('Settings'),
],
/*
* Automatically discovered settings classes can be cached, so they don't
* need to be searched each time the application boots up.
*/
'discovered_settings_cache_path' => base_path('bootstrap/cache'),
];
use Spatie\LaravelSettings\Settings;
class GeneralSettings extends Settings
{
public string $site_name;
public bool $site_active;
public static function group(): string
{
return 'general';
}
}
/*
* Each settings class used in your application must be registered, you can
* add them (manually) here.
*/
'settings' => [
GeneralSettings::class
],
use Spatie\LaravelSettings\Migrations\SettingsMigration;
class CreateGeneralSettings extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('general.site_name', 'Spatie');
$this->migrator->add('general.site_active', true);
}
}
class IndexController
{
public function __invoke(GeneralSettings $settings){
return view('index', [
'site_name' => $settings->site_name,
]);
}
}
function getName(): string{
return app(GeneralSettings::class)->site_name;
}
class SettingsController
{
public function __invoke(GeneralSettings $settings, GeneralSettingsRequest $request){
$settings->site_name = $request->input('site_name');
$settings->site_active = $request->boolean('site_active');
$settings->save();
return redirect()->back();
}
}
class GeneralSettings extends Settings
{
public string $site_name;
public bool $site_active;
public static function group(): string
{
return 'general';
}
public static function repository(): ?string
{
return 'global_settings';
}
}
use Spatie\LaravelSettings\Migrations\SettingsMigration;
class CreateGeneralSettings extends SettingsMigration
{
public function up(): void
{
}
}
public function up(): void
{
$this->migrator->add('general.timezone', 'Europe/Brussels');
}
public function up(): void
{
$this->migrator->rename('general.timezone', 'general.local_timezone');
}
public function up(): void
{
$this->migrator->rename('general.timezone', 'country.timezone');
}
public function up(): void
{
$this->migrator->update(
'general.timezone',
fn(string $timezone) => return 'America/New_York'
);
}
public function up(): void
{
$this->migrator->delete('general.timezone');
}
public function up(): void
{
if ($this->migrator->exists('general.timezone')) {
// do something
}
}
public function up(): void
{
$this->migrator->inGroup('general', function (SettingsBlueprint $blueprint): void {
$blueprint->add('timezone', 'Europe/Brussels');
$blueprint->rename('timezone', 'local_timezone');
$blueprint->update('timezone', fn(string $timezone) => return 'America/New_York');
$blueprint->delete('timezone');
});
}
class RegularTypeSettings extends Settings
{
public string $a_string;
public bool $a_bool;
public int $an_int;
public float $a_float;
public array $an_array;
public static function group(): string
{
return 'regular_type';
}
}
class DateSettings extends Settings
{
public DateTime $birth_date;
public static function group(): string
{
return 'date';
}
public static function casts(): array
{
return [
'birth_date' => DateTimeInterfaceCast::class
];
}
}
class DateSettings extends Settings
{
public $birth_date;
public static function group(): string
{
return 'date';
}
public static function casts(): array
{
return [
'birth_date' => new DateTimeInterfaceWithTimeZoneCast(DateTime::class, 'Europe/Brussels')
];
}
}
class DateSettings extends Settings
{
public $birth_date;
public static function group(): string
{
return 'date';
}
public static function casts(): array
{
return [
'birth_date' => DateTimeInterfaceCast::class.':'.DateTime::class
];
}
}
class DateSettings extends Settings
{
public DateTime $birth_date;
public static function group(): string
{
return 'date';
}
}
class DateSettings extends Settings
{
public DateTime $birth_date;
public ?int $a_nullable_int;
public static function group(): string
{
return 'date';
}
}
class DateSettings extends Settings
{
/** @var \DateTime */
public $birth_date;
/** @var ?int */
public $a_nullable_int;
/** @var int|null */
public $another_nullable_int;
/** @var int[]|null */
public $an_array_of_ints_or_null;
public static function group(): string
{
return 'date';
}
}
class DateSettings extends Settings
{
/** @var array<\DateTime> */
public array $birth_dates;
// OR
/** @var \DateTime[] */
public array $birth_dates_alternative;
public static function group(): string
{
return 'date';
}
}
class GeneralSettings extends Settings
{
public string $site_name;
public bool $site_active;
public static function group(): string
{
return 'general';
}
public static function encrypted(): array
{
return [
'site_name'
];
}
}
public function up(): void
{
$this->migrator->addEncrypted('general.site_name', 'Spatie');
}
public function up(): void
{
$this->migrator->updateEncrypted(
'general.site_name',
fn(string $siteName) => return 'Space'
);
}
public function up(): void
{
$this->migrator->add('general.site_name', 'Spatie');
$this->migrator->encrypt('general.site_name');
}
public function up(): void
{
$this->migrator->addEncrypted('general.site_name', 'Spatie');
$this->migrator->decrypt('general.site_name');
}
interface SettingsCast
{
/**
* Will be used to when retrieving a value from the repository, and
* inserting it into the settings class.
*/
public function get($payload);
/**
* Will be used to when retrieving a value from the settings class, and
* inserting it into the repository.
*/
public function set($payload);
}
class DtoCast implements SettingsCast
{
private string $type;
public function __construct(?string $type)
{
$this->type = $type;
}
public function get($payload): Data
{
return $this->type::from($payload);
}
public function set($payload): array
{
return $payload->toArray();
}
}
// By the type of property
class CastSettings extends Settings
{
public DateTime $birth_date;
public static function casts(): array
{
return [
'birth_date' => DateTimeInterfaceCast::class
];
}
...
}
// By the docblock of a property
class CastSettings extends Settings
{
/** @var \DateTime */
public $birth_date;
public static function casts(): array
{
return [
'birth_date' => DateTimeInterfaceCast::class
];
}
...
}
// By explicit definition
class CastSettings extends Settings
{
public $birth_date;
public static function casts(): array
{
return [
'birth_date' => DateTimeInterfaceCast::class.':'.DateTime::class
];
}
...
}
class CastSettings extends Settings
{
public $birth_date;
public static function casts(): array
{
return [
'birth_date' => DateTimeWthTimeZoneInterfaceCast::class.':'.DateTime::class.',Europe/Brussels'
];
}
...
}
class CastSettings extends Settings
{
public $birth_date;
public static function casts(): array
{
return [
'birth_date' => new DateTimeWthTimeZoneInterfaceCast(DateTime::class, 'Europe/Brussels')
];
}
...
}
interface SettingsRepository
{
/**
* Get all the properties in the repository for a single group
*/
public function getPropertiesInGroup(string $group): array;
/**
* Check if a property exists in a group
*/
public function checkIfPropertyExists(string $group, string $name): bool;
/**
* Get the payload of a property
*/
public function getPropertyPayload(string $group, string $name);
/**
* Create a property within a group with a payload
*/
public function createProperty(string $group, string $name, $payload): void;
/**
* Update the payloads of properties within a group.
*/
public function updatePropertiesPayload(string $group, array $properties): void;
/**
* Delete a property from a group
*/
public function deleteProperty(string $group, string $name): void;
/**
* Lock a set of properties for a specific group
*/
public function lockProperties(string $group, array $properties): void;
/**
* Unlock a set of properties for a group
*/
public function unlockProperties(string $group, array $properties): void;
/**
* Get all the locked properties within a group
*/
public function getLockedProperties(string $group): array;
}