定制 jhesyong/laravel-attribute 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jhesyong/laravel-attribute

最新稳定版本:v1.1.1

Composer 安装命令:

composer require jhesyong/laravel-attribute

包简介

Create attribute options for building select, radio, checkbox. Validate form with your attributes.

README 文档

README

Create attribute options for building select, radio, checkbox. Validate form with your attributes.

##Installation

Composer

Add the following to composer.json, and do composer update.

"require": {
    ...
    "jhesyong/laravel-attribute": "~1.0"
}

Service Provider

Add the following to the app config

'providers' => [
    ...
    Jhesyong\Attribute\AttributeServiceProvider::class,
    ...
],

Facade

Add the following to the app config

'aliases' => [
    ...
    'Attr'      => Jhesyong\Attribute\Facades\Attr::class,
    ...
],

Create Attributes

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);

Usage

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.

Has Key

// return true
Attr::hasKey('mammal_animal, 'cat');

// return false
Attr::hasKey('mammal_animal, 'bird');

Label

// return 'Cat'
Attr::label('mammal_animal', 'cat');

Hash Array

// 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---');

Pair Array

// 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---');

Keys

// return ['cat', 'dog', 'elephant'];
Attr::keys('mammal_animal');

Validation

You can validate the form input to be an attribute option. Use attr as the rule name.

'animal_type' => 'required|attr:mammal_animal',

Context

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',

统计信息

  • 总下载量: 190
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固