PHPackages                             jhesyong/laravel-attribute - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. jhesyong/laravel-attribute

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

jhesyong/laravel-attribute
==========================

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

v1.1.1(10y ago)41901[1 PRs](https://github.com/jhesyong/laravel-attribute/pulls)MITPHPPHP &gt;=5.5.9

Since Sep 18Pushed 7y ago1 watchersCompare

[ Source](https://github.com/jhesyong/laravel-attribute)[ Packagist](https://packagist.org/packages/jhesyong/laravel-attribute)[ RSS](/packages/jhesyong-laravel-attribute/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (0)

laravel-attribute
=================

[](#laravel-attribute)

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

\##Installation

### Composer

[](#composer)

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

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

```

### Service Provider

[](#service-provider)

Add the following to the app config

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

```

### Facade

[](#facade)

Add the following to the app config

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

```

Create Attributes
-----------------

[](#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
-----

[](#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

[](#has-key)

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

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

```

### Label

[](#label)

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

```

### Hash Array

[](#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

[](#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

[](#keys)

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

```

Validation
----------

[](#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
-------

[](#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',

```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Total

7

Last Release

3841d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/254ed8d0824071feca5804dba9b68935974e155ddbc8289959e7828c66b7567b?d=identicon)[jhesyong](/maintainers/jhesyong)

---

Top Contributors

[![jhesyong](https://avatars.githubusercontent.com/u/8470147?v=4)](https://github.com/jhesyong "jhesyong (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jhesyong-laravel-attribute/health.svg)

```
[![Health](https://phpackages.com/badges/jhesyong-laravel-attribute/health.svg)](https://phpackages.com/packages/jhesyong-laravel-attribute)
```

###  Alternatives

[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[sunspikes/clamav-validator

Custom Laravel 5 anti-virus validator for file uploads.

3651.8M3](/packages/sunspikes-clamav-validator)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
