1. Go to this page and download the library: Download bonsaicms/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/ */
bonsaicms / settings example snippets
Settings::set('theme', 'dark');
Settings::set('limits', ['upload' => 10, 'daily' => 100]);
Settings::set('owner', User::first()); // an Eloquent model
Settings::save();
Settings::get('owner')->is($user); // true — same model, re-fetched from the DB
Settings::set('theme', 'dark'); // in memory only — the store is untouched
Settings::get('theme'); // 'dark'
// ... request ends here without save() → the change is lost
Settings::set('theme', 'dark');
Settings::save(); // now it is in the store
Settings::get('never-set'); // null
Settings::has('never-set'); // false
Settings::set('theme', null); // this is a delete
Settings::save(); // the entry is removed from the store
use Illuminate\Database\Eloquent\Model;
use BonsaiCms\Settings\Contracts\SerializationWrappable;
use BonsaiCms\Settings\Models\SerializableModelTrait;
class MyModel extends Model implements SerializationWrappable
{
use SerializableModelTrait;
}
$model = MyModel::first();
Settings::set('model', $model);
Settings::save();
// Same request, or any later one:
Settings::get('model')->is($model); // true
interface SerializationWrappable
{
static function wrapBeforeSerialization($wrappable);
static function unwrapAfterSerialization($wrappedClass, $wrappedValue);
}
use BonsaiCms\Settings\Contracts\SerializationWrappable;
class Money implements SerializationWrappable
{
public function __construct(
public readonly int $amount,
public readonly string $currency,
) {}
static function wrapBeforeSerialization($wrappable)
{
return [
'amount' => $wrappable->amount,
'currency' => $wrappable->currency,
];
}
static function unwrapAfterSerialization($wrappedClass, $wrappedValue)
{
return new Money($wrappedValue['amount'], $wrappedValue['currency']);
}
}
Settings::set('price', new Money(1000, 'EUR'));
Settings::save();
Settings::get('price'); // Money(1000, 'EUR') — a new instance with the same state
use BonsaiCms\Settings\Contracts\SettingsRepositoryFactory;
$repository = app(SettingsRepositoryFactory::class)->driver('tenant_two');
// Point the manager at it — note this shares the manager's cache, so refresh() first
Settings::refresh();
Settings::setRepository($repository);