Dynamic Settings is a Laravel / Livewire package that allows you to manage custom settings of your application dynamically.
Let's say you have several notifications to different mailing lists, but you would like to be able to change whether they are sent or not, or which email they are sent to without having to modify the code or the env file.
This is where Dynamic Settings comes in.
You can make any number of configurations, grouped and associated with each other, and then use their values ​​in the code. This way when sending an email, for example, $to will be DynSettings::get('admin.mail') and not "[email protected]".
- Laravel 10/11
- Livewire 3 (*)
- Wire UI 2 (**)
(*) Livewire 3 is required only for the component to manage the settings. You can always ignore that and make your own.
(**) As said before, WireUI is a great package for those components, we included a normal view and a WireUI view, they set up automatically base on your app.
composer require fantismic/dynamic-settings
After you install you need to publish the migration file...
php artisan vendor:publish --provider="Fantismic\\DynSettings\\Providers\\DynSettingsProvider" --tag="migrations"
...and run the migration.
php artisan migrate
That's it. You are ready to go!
You can publish the configuration file to customize behavior, such as full page component mode or preferred blade.
php artisan vendor:publish --provider="Fantismic\\DynSettings\\Providers\\DynSettingsProvider" --tag="config"
We provide a livewire component for manage the settings. You just need to include it like any other componenet wherever you want it.
<livewire:DynamicSettings />
You can set the component to render in fullpage in the config file. If you do, you should add the route in your routes/web.php
<?php
use Fantismic\DynSettings\Livewire\DynamicSettingsComponent;
Route::get('/fullpageSettings',DynamicSettingsComponent::class);
We provide one or two methods in the facade.
use Fantismic\DynSettings\Facades\DynSettings;
Lets say you have these keys:
notifications.send
notifications.alwayscc.send
notificactions.alwayscc.emails
- Get all settings
- Get all settings as array
- Get all settings as object
- Get all settings as dotted array
- Get setting value
- Get setting data
- Get setting model
- Get all groups
- Get associations by group
- Get settings by group
- Set a value
- Get settings by group
- Update groupname
- Update description
- Update association
- Delete setting
- Delete setting by key
- Update groupname
DynSettings::all(): (array)
> DynSettings::all()
= [
[
"id" => 1,
"key" => "notifications.send",
"value" => "true",
"type" => "bool",
"name" => "Send email notifications",
"description" => "Check this item in order to active all notifications.",
"group" => "Notifications",
"associate_with" => "General",
],
[
"id" => 2,
"key" => "notifications.alwayscc.send",
"value" => "true",
"type" => "bool",
"name" => "Send always CC",
"description" => "Add to all emails a carbon copy",
"group" => "Notifications",
"associate_with" => "Always CC",
],
[
"id" => 3,
"key" => "notifications.alwayscc.emails",
"value" => "["[email protected]","[email protected]"]",
"type" => "array",
"name" => "CC email list",
"description" => "List of emails that should recibe a copy for every send mail",
"group" => "Notifications",
"associate_with" => "Always CC",
],
]
>
DynSettings::getArray(): (array)
> DynSettings::getArray()
= [
"notifications" => [
"send" => true,
"alwayscc" => [
"send" => true,
"emails" => [
"[email protected]",
"[email protected]",
],
],
],
]
DynSettings::getObject(): (object)
> DynSettings::getObject()
= {#1
+"notifications": {#2
+"send": true,
+"alwayscc": {#3
+"send": true,
+"emails": [
"[email protected]",
"[email protected]",
],
},
},
}
DynSettings::getDot(): (array)
> DynSettings::getDot()
= [
"notifications.send" => true,
"notifications.alwayscc.send" => true,
"notifications.alwayscc.emails" => [
"[email protected]",
"[email protected]",
],
]
DynSettings::get( (string) $key): (mixed)
> DynSettings::get('notifications.alwayscc.send')
= true
------------------------------------------------------------
> DynSettings::get('notifications.alwayscc.emails')
= [
"[email protected]",
"[email protected]",
]
DynSettings::getKeyData( (string) $key): (array)
> DynSettings::getKeyData('notifications.alwayscc.send')
= {#5459
+"id": 2,
+"key": "notifications.alwayscc.send",
+"value": "true",
+"type": "bool",
+"name": "Send always CC",
+"description": "Add to all emails a carbon copy",
+"group": "Notifications",
+"associate_with": "Always CC",
}
DynSettings::getModel( (string) $key): (Eloquent Model)
> DynSettings::getModel('notifications.alwayscc.send')
= Fantismic\DynSettings\Models\DynamicSettings {#
id: 2,
key: "notifications.alwayscc.send",
value: "true",
type: "bool",
name: "Send always CC",
description: "Add to all emails a carbon copy",
group: "Notifications",
associate_with: "Always CC",
}
>
DynSettings::getGroups(): (array)
> DynSettings::getGroups()
= [
"Notifications",
]
DynSettings::getAssocs( (string) $group): (array)
> DynSettings::getAssocs("Notifications")
= [
"General",
"Always CC",
]
DynSettings::getByGroup( (string) $group): (array)
> DynSettings::getByGroup("Notifications")
= [
[
"id" => 1,
"key" => "notifications.send",
"value" => "true",
"type" => "bool",
"name" => "Send email notifications",
"description" => "Check this item in order to active all notifications.",
"group" => "Notifications",
"associate_with" => "General",
],
[
"id" => 2,
"key" => "notifications.alwayscc.send",
"value" => "false",
"type" => "bool",
"name" => "Send always CC",
"description" => "Add to all emails a carbon copy",
"group" => "Notifications",
"associate_with" => "Always CC",
],
[
"id" => 3,
"key" => "notifications.alwayscc.emails",
"value" => "["[email protected]","[email protected]"]",
"type" => "array",
"name" => "CC email list",
"description" => "List of emails that should recibe a copy for every send mail",
"group" => "Notifications",
"associate_with" => "Always CC",
],
]
------------------------------------------------------------
DynSettings::set( (string) $group, (mixed) $value): (bool)
> DynSettings::set('notifications.alwayscc.send',false)
= true
DynSettings::updateName( (string) $key, (string) $newName): (bool)
> DynSettings::updateName('notifications.alwayscc.send','Always send carbon copy')
= true
DynSettings::updateDescription( (string) $key, (string) $newDescription): (bool)
> DynSettings::updateDescription('notifications.alwayscc.send','Set CC on every outgoing mail')
= true
DynSettings::updateAssoc( (string) $key, (string) $newAssoc): (bool)
> DynSettings::updateAssoc('notifications.alwayscc.send','CC')
= true
DynSettings::delete( (int) $id)**: (bool)
> DynSettings::delete(3)
= true
DynSettings::deleteByKey( (string) $key)**: (bool)
> DynSettings::deleteByKey('notifications.alwayscc.send')
= true
DynSettings::updateGroupName( (string) $oldName, (string) $newName): (bool)
> DynSettings::updateGroupName("Notifications", "Mailing")
= true
DynSettings::is( (string) $key, (mixed) $value): (bool)
> DynSettings::is('notifications.alwayscc.send', false)
= true
DynSettings::should( (string) $key): (bool)
> DynSettings::should('notifications.alwayscc.send')
= false
DynSettings::has( (string) $key, (array) $value, [(bool) strict = false]): (bool)
> DynSettings::has('notifications.alwayscc.email', '[email protected]')
= true
> DynSettings::has('notifications.alwayscc.email', '[email protected]')
= false
> DynSettings::has('notifications.alwayscc.email', '[email protected]', false)
= true
> DynSettings::has('notifications.alwayscc.email', '[email protected]')
= false