Download the PHP package zachleigh/laravel-property-bag without Composer
On this page you can find all versions of the php package zachleigh/laravel-property-bag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zachleigh/laravel-property-bag
More information about zachleigh/laravel-property-bag
Files in zachleigh/laravel-property-bag
Package laravel-property-bag
Short Description Easy Laravel user settings using a property bag
License MIT
Homepage https://github.com/zachleigh/laravel-property-bag
Informations about the package laravel-property-bag
Laravel Property Bag
Simple settings for Laravel apps.
- Easily give multiple resources settings
- Simple to add additional settings as your app grows
- Set default settings and limit setting values for security
- Fully configurable
Contents
- Upgrade Information
- About
- Install
- Usage
- Methods
- Validation Rules
- Advanced Configuration
- Contributing
Upgrade Information
From 1.3.* to 1.4.0
1.4.0 drops support for PHP 7.1 and adds support for Laravel 6.
From 1.2.* to 1.3.0
1.3.0 drops support for PHP 7.0.
From 1.1.* to 1.2.0
1.2.0 drops support for PHP 5.6. It will likely still work in older PHP versions, but bugs or issues that focus specifically on 5.6 or lower will no longer be dealt with. Support for PHP 7.2 added.
From 1.0.* to 1.1.0
1.1.0 adds Lumen support and support for Laravel 5.4 (thanks tzurbaev). There should be no breaking changes. If using Laravel 5.3, please use Version 1.0.5:
From 0.9.* to 1.0.0
Version 1.0.0 brings major changes to the package that make it incompatible with previous versions. The package was essentially rewritten making upgrade from 0.9.7 to 1.0.0 difficult at best.
About
Laravel Property Bag gives your application resources savable, secure settings by using a single database property bag table. The benefit of using this kind of settings table, as opposed to say a json blob column on the resource table, is that if in the future you decide to change a setting value, a simple database query can easily take care of it.
Install
1. Install through composer
Laravel Installation
a. Register the service provider
In Laravel's config/app.php file, add the service provider to the array with the 'providers' key.
b. Publish the migration
Lumen Installation
a. Enable Eloquent
If you haven't already done so, find and uncomment $app->withEloquent()
in app/boostrap.php
.
b. Register the service provider
In Lumen's app/bootstrap.php file, add the service provider:
c. Copy migration file
Since Lumen doesn't offer the php artisan vendor:publish
command, you have to copy the migration file manually from the vendor/zachleigh/laravel-property-bag/src/Migrations
directory to the database/migrations
directory.
2. Run the migration
3. Create a new settings config file for your resource.
{resource} should be the name of the model you wish to add settings to. For example:
This will create a Settings directory containing a UserSettings class where you can configure your settings for the User class.
Usage
1. Use the trait in the model.
2. Register your settings plus their allowed values and defaults
After publishing the UserSettings file (hopefully you did this above), register settings in the UserSettings class.
Each setting must contain an array of allowed values and a default value. It is also possible to use validation rules instead of hardcoding allowed values.
3. Set the setting from the user model
Set multiple values at a time
4. Get the set value from the user model
If the value has not been set, the registered default value will be returned. Note that default values are not stored in the database in order to limit database size.
Methods
get($key)
Get value for given key.
set($array)
Set array keys to associated values. Values may be of any type. Returns Settings.
When a default value is passed to set(), it will not be stored in the database. Don't be alarmed if your default values aren't showing up in the table.
If a value is not registered in the allowed values array, a LaravelPropertyBag\Exceptions\InvalidSettingsValue
exception will be thrown. You can use the $e->getFailedKey()
method to retrieve the failed setting name.
getDefault($key)
Get default value for given key.
allDefaults()
Get all the default values for registered settings. Returns collection.
getAllowed($key)
Get allowed values for given key. Returns collection.
allAllowed()
Get all allowed values for registered settings. Returns collection.
isDefault($key, $value)
Return true if given value is the default value for given key.
isValid($key, $value)
Return true if given value is allowed for given key.
all()
Return all setting value's for model. Returns collection.
keyIs($key, $value)
Return true if setting for given key equals given value.
reset($key)
Reset key to default value.
withSetting($key, $value = null)
Get an array with all stored rows with a given setting and/or value.
Validation Rules
Rather than hardcoding values in an array, it is also possible to define rules that determine whether a setting value is valid. Rules are always strings and must contain a colon at both the beginning and ending of the string.
In this case, the setting value saved for the 'integer' key must be an integer.
Some rules require parameters. Parameters can be passed in the rule definition by using an equal sign and a comma separated list.
Available Rules
':any:'
Any value will be accepted.
':alpha:'
Alphabetic values will be accepted.
':alphanum:'
Alphanumeric values will be accepted.
':bool:'
Boolean values will be accepted.
':int:'
Integer values will be accepted.
':num:'
Numeric values will be accepted.
':range=low,high:'
Numeric values falling between or inluding the given low and high parameters will be accpeted. Example:
The numbers 1 to 10 will be allowed.
':string:'
Strings will be accepted.
User Defined Rules
To make user defined rules, first publish the Rules file to Settings/Resources/Rules.php:
Rule validation methods should be named by prepending 'rule' to the rule name. For example, if our rule is 'example', we would define it in the settings config file like this:
And then our method would be called 'ruleExample':
All rule methods should be static and thus should not care about object or application state. If your rule requires parameters, accept them as arguments to the method.
Another option would be to validate input with Laravel's built in validation, which is much more complete than what this package offers, and then set all your setting allowed values to ':any:'.
Advanced Configuration
Laravel Property Bag gives you several ways to configure the package to fit your needs and wants.
I don't want to register settings as an array
Cool. I get it. Especially if you have dozens of settings, dealing with an array can be annoying. In the model settings config file, add the registeredSettings method.
In this method, do whatever you want and return a collection of items that has the same structure as the registeredSettings array.
I want to use dynamic allowed and default values.
No problem. Like in the above section, create your own registeredSettings method in the settings config file and return a collection of registered settings.
The allGroupNames function simply returns an array of group names:
You can also access the model the settings are attached to with the getResource()
method.
Contributing
Contributions are more than welcome. Fork, improve and make a pull request. For bugs, ideas for improvement or other, please create an issue.