Package Data | |
---|---|
Maintainer Username: | jhesyong |
Maintainer Contact: | jhesyong@gmail.com (Jhesyong Jiang) |
Package Create Date: | 2015-09-18 |
Package Last Update: | 2018-12-06 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:22:32 |
Package Statistics | |
---|---|
Total Downloads: | 183 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 1 |
Create attribute options for building select, radio, checkbox. Validate form with your attributes.
##Installation
Add the following to composer.json
, and do composer update
.
"require": {
...
"jhesyong/laravel-attribute": "~1.0"
}
Add the following to the app config
'providers' => [
...
Jhesyong\Attribute\AttributeServiceProvider::class,
...
],
Add the following to the app config
'aliases' => [
...
'Attr' => Jhesyong\Attribute\Facades\Attr::class,
...
],
To make a class an attribute, use the AttributeTrait
and define
the getOptions()
method.
namespace Acme;
use Jhesyong\Attribute\AttributeTrait;
class MammalAnimal
{
use AttributeTrait;
protected function getOptions()
{
return ['cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant'];
}
}
Then, register your attributes in your application.
You can do the following in service provider's boot()
method.
// Registered as "mammal_animal" by default
$this->app['attr']->register(new \Acme\MammalAnimal);
// To specify the name, pass the name to the register method.
$this->app['attr']->register(new \Acme\MammalAnimal, 'animal');
Or register via the facade.
Attr::register(new \Acme\MammalAnimal);
You can use all public methods in the AttributeTrait
.
Also, you can also define your own methods and then they can be called via the facade.
The facade forwards the method call to the attribute specified in the first argument.
The rest arguments are passed the attribute method.
// return true
Attr::hasKey('mammal_animal, 'cat');
// return false
Attr::hasKey('mammal_animal, 'bird');
// return 'Cat'
Attr::label('mammal_animal', 'cat');
// Useful for building select, radio, checkbox, etc.
// return ['cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal');
// To add an empty option, pass true as the second argument.
// return ['' => 'Please Select', cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal', true);
// To customize the empty message, pass it as the third argument.
// return ['' => '---Please Select---', cat' => 'Cat', 'dog' => 'Dog', 'elephant' => 'Elephant']
Attr::hashArray('mammal_animal', true, '---Please Select---');
// return [['label' => 'Cat', 'value' => 'cat'], ...];
Attr::pairArray('mammal_animal');
// To add an empty option, pass true as the second argument.
Attr::pairArray('mammal_animal', true);
// To customize the empty message, pass it as the third argument.
Attr::pairArray('mammal_animal', true, '---Please Select---');
// return ['cat', 'dog', 'elephant'];
Attr::keys('mammal_animal');
You can validate the form input to be an attribute option.
Use attr
as the rule name.
'animal_type' => 'required|attr:mammal_animal',
Sometimes you may want to get different options according to the certain condition. For example, you want to get options from database and filter options by some column value.
You can change getOptions()
to getOptions($context = null)
and return different options according to the $context
.
For example,
class Fruit
{
use AttributeTrait;
protected function getOptions($context = null)
{
$category = [
'sour' => ['lemon' => 'Lemon', 'grape' => 'Grape', 'kiwi' => 'Kiwi'],
'sweet' => ['banana' => 'Banana', 'apple' => 'Apple'],
];
if (array_key_exists($context, $category)) {
return $category[$context];
}
return array_reduce($category, 'array_merge', []);
}
}
To get the options, pass the context first. The context only effects the next method call.
Attr::context('sour')->hashArray('food');
To validate data, add the context after the attribute name.
'sweet_fruit' => 'required|attr:fruit,sweet',