roundly-consulting / attributes-for-laravel
Attach multiple dynamic key/value attributes to any Eloquent model.
Package info
github.com/roundly-consulting/attributes-for-laravel
pkg:composer/roundly-consulting/attributes-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- roundly-consulting/crypto-for-laravel: ^1.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.5
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.4
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.1
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Attributes for Laravel
Attach dynamic, typed key/value attributes to any Eloquent model through one polymorphic table: no schema change per attribute, no EAV boilerplate. Values keep their real PHP type, an optional registry validates them, and query scopes filter owners by their attributes.
Installation
Requires PHP 8.4 and Laravel 12 or 13.
composer require roundly-consulting/attributes-for-laravel
php artisan vendor:publish --tag="attributes-migrations"
php artisan migrate
If your owner models have UUID/ULID keys, set ATTRIBUTES_KEY_TYPE before migrating.
Usage
Add the trait and its contract to any model:
use Illuminate\Database\Eloquent\Model; use RoundlyConsulting\Attributes\Contracts\HasAttributes as HasAttributesContract; use RoundlyConsulting\Attributes\Traits\HasAttributes; class Product extends Model implements HasAttributesContract { use HasAttributes; }
Then write, read and query typed attributes (definitions are optional):
use RoundlyConsulting\Attributes\DataTransferObjects\AttributeDefinitionData; use RoundlyConsulting\Attributes\Enums\AttributeType; use RoundlyConsulting\Attributes\Facades\Attributes; Attributes::define(new AttributeDefinitionData( name: 'rating', type: AttributeType::Integer, rules: ['min:1', 'max:5'], )); Attributes::for($product)->set('color', 'red', meta: ['hex' => '#f00']); Attributes::for($product)->setMany(['rating' => '5', 'on_sale' => true]); // '5' is stored as 5 Attributes::for($product)->get('rating'); // 5 Attributes::for($product)->toKeyValue(); // ['color' => 'red', 'rating' => 5, 'on_sale' => true] Attributes::for($product)->set('rating', 9); // throws InvalidAttributeValueException Product::query()->whereAttribute('color', 'red')->whereAttributeBetween('rating', 4, 5)->get();
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/attributes-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). See LICENSE for details.