Download the PHP package artisan/laravel-settings without Composer
On this page you can find all versions of the php package artisan/laravel-settings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download artisan/laravel-settings
More information about artisan/laravel-settings
Files in artisan/laravel-settings
Package laravel-settings
Short Description A settings package for Laravel using property bags.
License MIT
Informations about the package laravel-settings
Settings Property Bag
This is a property bag for handling settings objects. This also has integration with Laravel database models out of the box.
Installation
You can install the package via composer:
Usage
A settings bag is a class where you can store your settings that can have fallback values if no explicit values where set. (i.e. persistence)
There are two different ways to get a value from the settings bag.
get($settings, $default = null)
Another is chaining public properties.
To get all the values from the settings bag.
Overriding the default values
Now that we can set values, we want to apply the persisted data to our default settings.
Somtimes, it's a hassle storing array values in the database:
- The database might not support it.
- We don't want to do an overly complex database via relational key value stuff.
You can instead store your nested settings values via dot notation and this package will destructure the value into a nested array.
Casting
There are times that the values that we pull out the database don't map to the actual data types we want them to be. For that we have a $casts
attribute to handle the mapping for all the primitives we need.
The only two properties available right now are: boolean
and json
.
BUT DON'T FRET! You can either help us out by adding more cast implementations or even make your own from the settings class! (The code is the same).
Adding custom casts
To add custom casts, in your custom settings class (or even a parent class for all your settings classes), create a method prefixed with as
.
Laravel Integration
Out of the box, we try to help the setup to a minimal for Laravel projects. After installing via composer, public the config file and migrations.
Run our new settings table migration.
To create the settings file:
And finally, add our settings trait to the model. The trait will automatically look for the settings file in the namespace of your config appended with the word Settings.
So if we have a User.php
, by default it will look for the App\Settings\UserSettings
class.
To change the class location, you can change the getSettingsClass
method in your model.
With that, you can now access your values via the settings
public property.
Saving to the database
To save a single value to the database.
To save multiple values at the same time: