Download the PHP package hackeresq/laravel-settings without Composer
On this page you can find all versions of the php package hackeresq/laravel-settings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-settings
Settings
Super simple key/value settings for Laravel that natively supports multi-tenancy.
- Installation
- Usage
- Set new setting
- Get all settings
- Get single setting
- Get certain setting (via array)
- Check if a setting is set
- Encryption
- Multi-tenancy
- Casting
- Disable cache
- Hidden settings
- Customize table name
Installation
This package can be used in Laravel 8.0+.
You can install the package via composer:
Since Laravel 5.5+, service providers and aliases will automatically get registered and you can skip this step. To use this package with older versions, please use release < 2.0.
You can publish the migration and config files, then migrate the new settings table all in one go, using:
Success! laravel-settings is now installed!
Usage
Settings can be accessed using the easy-to-remember Facade, Settings
.
Set new setting
You can set new settings using the "set" method, which accepts an associative array of one or more key/value pairs. For security reasons, this will first check to see if such a setting key is "fillable," which is a configuration option in the config/settings.php file.
If such a key exists in the config, it will update the key to the new value passed. If the key does not exist in the fillable config, it will disregard the change. So, if this is a fresh install, do not expect the following to work:
It will not set the new setting until you have either set the fillable fields in the config, or you have opted to force the setting. If you wish to force set a new setting, you should use the force()
method before calling the set()
method:
As of version 3.0.4, the global override for forcing settings has been removed from the config file for this package. Instead, you can use a wildcard for the fillable property, like this:
This is more in line with standard Laravel syntax (e.g. for models).
Get all settings
If no parameters are passed to the "get" method, it will return an array of all settings:
You can optionally hide specific settings using the hidden
config as described below.
Get single setting
You can return a single setting by passing a single setting key:
Get certain settings
You can also return a list of specified settings by passing an array of setting keys:
Check if a setting is set
Sometimes you can't know if a setting has been set or not (i.e. boolean settings that will return false if the setting does not exist, but also if the setting has been set to false).
Encryption
You can define keys that should be encrypted automatically within the config/settings.php file. To do so, add the keys as such:
Multi-tenancy
This package can be used in a multi-tenant environment. The has methods all read an internal 'tenant' attribute that can be set with the tenant()
method. You can set the 'tenant' attribute by calling the tenant()
method first, like this:
The 'tenant' attribute passed can be any alphanumeric string. The 'tenant' attribute can also be left blank to have, for example, settings saved to a so-called "central" tenant. Note: the 'tenant' attribute is not strictly typed, and will be passed to the database query as a string.
Casting
You can cast settings to native PHP types using the cast
option in the settings.php config. Here's an example of an array being cast to JSON and back to a native PHP array:
Which allows you to do the following:
Disable cache
Depending on your use case, you may like to disable the cache (enabled by default). Disable the cache by modifying the config/settings.php file as such:
Hidden settings
You may wish to hide specific settings (like API keys or other sensitive user data) from inadvertent disclosure. You can set these settings in the config/settings.php file. To do so, add the keys as such:
Once these are set, they must be specifically requested using the get()
method. In other words, this acts like the $hidden
attribute on Laravel Eloquent models.
In addition to hiding specific settings, you can opt to hide ALL the settings (unless specifically requested, of course). To do this, you can use a wildcard:
Customize table name
For some cases, it may be necessary to customize the name of the table where settings are stored. By default, the migrations that come with this package create a 'settings' table. If, for some reason, it becomes necessary to change the default table, you can set the 'table' option in the config/settings.php file, like this:
This configuration option is not included in the base config file as this is an edge case that is not commonly encountered; but nonetheless a nice convenience to have when it does come up.
Finally
Testing
You can run tests with the composer test
command.
Contributing
Feel free to create a fork and submit a pull request if you would like to contribute.
Bug reports
Raise an issue on GitHub if you notice something broken.