turag1955/laravel-settings is a Laravel package for app & models settings for laravel.
It currently has 0 GitHub stars and 8 downloads on Packagist (latest version v1.0.0).
Install it with composer require turag1955/laravel-settings.
Discover more Laravel packages by turag1955
or browse all Laravel packages to compare alternatives.
Last updated
This package allows you to store application wide and model specific Laravel settings. Settings values are type-cast and stored properly. You can also define your own casts and store repository.
Install the package via composer:
composer require turag1955/laravel-settings
Publish the config file with:
php artisan vendor:publish --provider="Smartisan\Settings\SettingsServiceProvider" --tag="config"
Publish the migration file with:
php artisan vendor:publish --provider="Smartisan\Settings\SettingsServiceProvider" --tag="migrations"
And finally run the migration with:
php artisan migrate
The package provides various APIs to access the settings manager. You can access it with Settings facade, settings() helper method and via HasSettings trait on your models.
You can set single entry, or multiple entries of settings. The values of objects will be cast according to the rules defined in settings.php config file.
Settings::set('key', 'value');
You can set multiple settings entries by passing an associative array of keys and values. Casts will be applied on all the payload, even on nested arrays.
Settings::set([
'k1' => Carbon::now(),
'k2' => [
'k3' => $user,
'k4' => [
'k5' => $model
],
]
]);
It's possible to categorize your settings into groups by calling group method.
Settings::group('name')->set('key', 'value');
Settings::group('name')->set([
'k1' => 'v1',
'k2' => 'v2',
]);
It's also possible to set settings for a specific model by calling for method
Settings::for($model)->set('key', 'value');
Settings::for($model)->set([
'k1' => 'v1',
'k2' => 'v2',
]);
You can mix all filters together like this:
Settings::for($model)->group('name')->set('key', 'value');
You can retrieve one or multiple entries and specify the default value if not exist.
Settings::get('k1', 'default');
If the entry key does not exist, the default value will be placed instead
Settings::get(['k1', 'k2', 'k3'], 'default');
If you want to retrieve all entries, you simply call all method. You can also specify the model or group. Also to excempt some specific keys.
Note: Remember that retrieving all entries without specifying the group or model, will retrieve all entries that has no group or model set. You can consider these as (global app settings).
Settings::all();
Settings::for($model)->all();
Settings::for($model)->group('name')->all();
Settings::except('k1', 'k2')->for($model)->group('name')->all();
Settings::except('k1')->for($model)->group('name')->all();
You can remove entries by calling forget method.
Settings::forget('key');
Settings::for($model)->group('name')->forget('key');
You can determine whether the given settings entry key exists or not
Settings::exist('key');
Settings::for($model)->exist('key');
Settings::for($model)->group('name')->exist('key');
The package also ships with a settings helper method, you can use it instead of using Settings Facade
settings()->set('key', 'value');
...
You can use HasSettings trait on your Eloquent models as well
use Smartisan\Settings\HasSettings;
class User extends Model
{
use HasSettings;
}
Settings::for($model)$user->settings()->set('k1', 'v1');
...
If you don't want to use the database repository, you can easily create your own settings repository. To do that
use Smartisan\Settings\Contracts\Repository;
class FileRepository implements Repository
{
public function get($key,$default = null) {
//
}
public function set($key,$value = null) {
//
}
public function forget($key) {
//
}
public function all() {
//
}
}
'repositories' => [
...
'file' => [
...
]
],
The package allows you to easily create your own casting rules of any object type.
Note: The set method is called when the value of the entries is being stored to the repository, and the get method is called when the value is being retrieved from the repository.
use Smartisan\Settings\Contracts\Castable;
class CustomCast implements Castable
{
public function set($payload) {
//
}
public function get($payload) {
//
}
}
'casts' => [
...
CustomDataType::class => CustomCast::class
]
Note: If you want to pass a parameter to your cast, you can set an object of the cast instead of cast class name
'casts' => [
...
CustomDataType::class => new CustomCast('param')
]
You can easily enable or disable caching settings in settings.php config file. You can also specify which caching store to use
'cache' => [
'enabled' => env('SETTINGS_CACHE_ENABLED', false),
'store' => null,
'prefix' => null,
],
To run the package's tests, simply call the following command
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.