PHPackages                             pwrdk/laravel-custom-attributes - 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. pwrdk/laravel-custom-attributes

ActiveLibrary

pwrdk/laravel-custom-attributes
===============================

Custom Attributes Package for Eloquent Models

v0.6.6(4y ago)4638[2 issues](https://github.com/pwrdk/laravel-custom-attributes/issues)MITPHPPHP ^7.3|^8.0CI failing

Since Nov 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/pwrdk/laravel-custom-attributes)[ Packagist](https://packagist.org/packages/pwrdk/laravel-custom-attributes)[ RSS](/packages/pwrdk-laravel-custom-attributes/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (46)Used By (0)

laravel-custom-attributes
=========================

[](#laravel-custom-attributes)

A custom attributes package for Eloquent Models.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require pwrdk/laravel-custom-attributes
```

Migrate
-------

[](#migrate)

```
php artisan:migrate
```

Seed
----

[](#seed)

Install basic Attribute Types (text, number, boolean, datetime and contact info)

```
php artisan db:seed --class="PWRDK\CustomAttributes\Seeds\DatabaseSeeder"
```

Publish config file

```
php artisan vendor:publish --provider="PWRDK\CustomAttributes\CustomAttributesServiceProvider"
```

Usage
-----

[](#usage)

Add HasCustomAttributes trait to Any Eloquent Model

```
use PWRDK\CustomAttributes\HasCustomAttributes;
use HasCustomAttributes;
```

Create a new attribute key

```
use PWRDK\CustomAttributes\CustomAttributes;
CustomAttributes::createKey('is_active', 'Is Active', 'boolean', true);
CustomAttributes::createKey('last_seen', 'Last seen', 'datetime', true);
CustomAttributes::createKey('favourite_colours', 'Favourite Colours', 'text', false);
```

List all available keys:

```
AttributeKey::all()->pluck('handle', 'display_name');
```

Attach the attribute to the Model

```
$user->attr()->set('is_active', true);
$user->attr()->set('last_seen', now());
$user->attr()->set('favourite_colours','red');
$user->attr()->set('favourite_colours','green');
$user->attr()->set('favourite_colours','blue');
```

Get an attribute by handle

```
$user->attr()->is_active;
=> Illuminate\Support\Collection {#962
     all: [
       [
         "key" => "is_active",
         "value" => 1,
         "created_at" => Illuminate\Support\Carbon @1575184861 {#956
           date: 2019-12-01 07:21:01.0 UTC (+00:00),
         },
         "id" => 56,
       ],
     ],
   }

$user->attr()->last_seen;
=> Illuminate\Support\Collection {#962
     all: [
       [
         "key" => "last_seen",
         "value" => Illuminate\Support\Carbon @1574796362 {#960
           date: 2019-11-26 19:26:02.0 UTC (+00:00),
         },
         "created_at" => Illuminate\Support\Carbon @1575184900 {#956
           date: 2019-12-01 07:21:40.0 UTC (+00:00),
         },
         "id" => 57,
       ],
     ],
   }

$user->attr()->favourite_colours;
=> Illuminate\Support\Collection {#962
     all: [
       [
         "key" => "favourite_colours",
         "value" => "orange",
         "created_at" => Illuminate\Support\Carbon @1575184900 {#956
           date: 2019-12-01 07:21:40.0 UTC (+00:00),
         },
         "id" => 58,
         "creator" => App\User {#983
           id: 1,
           name: "Rudolf Schwann",
           email: "schwann@damernesmagasin.com",
           email_verified_at: null,
           created_at: "2019-11-26 08:42:25",
           updated_at: "2019-11-26 08:42:25",
         },
       ],
       [
         "key" => "favourite_colours",
         "value" => "green",
         "created_at" => Illuminate\Support\Carbon @1575184900 {#981
           date: 2019-12-01 07:21:40.0 UTC (+00:00),
         },
         "id" => 59,
         "creator" => App\User {#983},
       ],
       [
         "key" => "favourite_colours",
         "value" => "purple",
         "created_at" => Illuminate\Support\Carbon @1575184900 {#988
           date: 2019-12-01 07:21:40.0 UTC (+00:00),
         },
         "id" => 60,
         "creator" => App\User {#983},
       ],
     ],
   }
```

Unsetting an attribute

```
$user->attr()->unset('is_active');
```

Updating an attribute is done by referencing the ID directly

```
$user->attr()->update(58, ['value' => 'cyan']);
=> PWRDK\CustomAttributes\Models\AttributeTypes\AttributeTypeDefault {#975
     value: "yellow",
   }
```

Using local AttributeTypes
--------------------------

[](#using-local-attributetypes)

- Create new model called CustomAttributes in your own models directory
- make a migration for the attribute type.

```
art make:migration create_values_type_prices_table
```

Create a new attribute key type

```
AttributeType::create(['handle' => 'prices', 'display_name' => 'Prices in different currencies']);
```

Extend CustomAttributes model from the package and add the new attribute type relationships

```
class CustomAttribute extends CustomAttributeModel
{
    public function attributeTypeScheduleEventOverlay()
    {
        return $this->hasOne(AttributeTypeScheduleEventOverlay::class);
    }
}
```

Add the `HasLocalCustomAttributeType` interface to the model(s)

```
class Product extends Model implements HasLocalCustomAttributeType
{
  ...
}
```

Set the values for the attributes

```
$product = App\Models\Product::find(1000);
$product->attr()->set('prices', ['usd' => 100, 'eur' => 100, 'gbp' => 90, 'jpy' => 10695]);
```

Caching attribute values:
-------------------------

[](#caching-attribute-values)

Add the `HasLocalCustomAttributeType` interface to the model(s)

```
class Product extends Model implements UsesCustomAttributesCaching, HasLocalCustomAttributeType
{
  ...
}
```

###  Health Score

28

—

LowBetter than 57% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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 ~17 days

Recently: every ~10 days

Total

45

Last Release

1529d ago

PHP version history (2 changes)v0.1.0PHP ^7.3

v0.6.0PHP ^7.3|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b75d2968216921164cacae4290e9548ae7db875e65c2a3bf21717542e74d4c1a?d=identicon)[pwrdk](/maintainers/pwrdk)

---

Top Contributors

[![pwrdk](https://avatars.githubusercontent.com/u/4727076?v=4)](https://github.com/pwrdk "pwrdk (85 commits)")

### Embed Badge

![Health badge](/badges/pwrdk-laravel-custom-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/pwrdk-laravel-custom-attributes/health.svg)](https://phpackages.com/packages/pwrdk-laravel-custom-attributes)
```

PHPackages © 2026

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