composer require ottosmops/settings
php artisan vendor:publish // --tag==migrations
If you want to rename the database table, you can do this within config/settings.php
.
php artisan migrate
The package installs a table settings
and uses an ordinary Model setting
. So all the standard methods are available. The helper setting('key')
and the static method Setting::getValue('key')
use an array which is Cached. If a setting is created, updated or deleted the cached is forgotten.
Along with key
and value
there are some more columns:
type
(not nullable!),scope
(string),editable
(casted to bool),rules
(string),description
(text).
If you set a new value, it is validated against the type and the rules. So the static method Setting::setValue('key', 'value')
is the prefered way to set values.
$setting = Setting::create(['key' => 'myKey',
'type' => 'string', // integer, bool or array
'scope' => 'mysetting',
'rules' => 'nullable|string',
'description' => 'My description']);
$setting = Setting::setValue('myKey', 'myValue');
echo $setting->value // myValue from the model
// from Cache
echo setting('key'); // myValue
echo Setting::getValue('key'); // myValue
Values can have different types: string
, boolean
, integer
, array
. To create a new setting you have to specify at least a type:
Setting::create(['key' => 'your_key', 'type' => 'integer']);
Setting::setValue('your_key', 37);
So if you now get a value it is casted to this type.
For retrieving a value there is a helper setting($key, $default= null)
which uses a cached array of settings.
You can set another table name in setting.php
.
https://github.com/saqueib/db-settings/blob/master/app/Setting/Setting.php