---
title: "Attributes for Laravel — docs | Roundly"
description: "Developer docs for Attributes for Laravel: typed key/value attributes, fluent builder, query scopes, definitions, constraints, encryption and history."
url: https://roundly-consulting.com/open-source/docs/attributes-for-laravel
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

Open source

# Attributes for Laravel

Install

`composer require roundly-consulting/attributes-for-laravel`

Requires: PHP ^8.4 · Laravel ^12.0|^13.0

[GitHub](https://github.com/roundly-consulting/attributes-for-laravel) [Packagist](https://packagist.org/packages/roundly-consulting/attributes-for-laravel) [Donate (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Patreon](https://www.patreon.com/cw/roundly)

## Overview

Dynamic, typed key/value attributes for any Eloquent model. Add one trait and a model gains a queryable set of custom fields — color, rating, on\_sale, published\_at — stored in a single polymorphic table, so a new field never needs a migration. Values come back as their real PHP type, and optional definitions add validation, defaults, unique and required constraints, encryption at rest and an audit trail. MIT-licensed and built only on Laravel plus three small Roundly packages — no third-party dependencies.

### What you get

#### Typed values

Strings, integers, floats, booleans, arrays and datetimes round-trip as their real PHP type — no manual casting.

#### Fluent builder & sync

Stage several attributes with metadata, then save them alongside the rest or sync them to an exact set — all or nothing, in one transaction.

#### Query scopes

Filter and order owner models by attribute — equality, sets, ranges, presence, null checks and ordering.

#### Definitions & constraints

Register known attributes globally or per model with Laravel rules, defaults, a strict mode and required and database-enforced unique constraints.

#### Facade, DI or actions

Attributes::for($model) for every read and write, an injectable AttributesManager or single-purpose actions — and Attributes::fake() records every write.

#### Encryption at rest

Mark an attribute encrypted and it is stored as Crypt ciphertext, decrypted transparently on read — unique constraints still apply.

#### Audit trail & events

Opt-in old→new revision history, plus after-commit events on attach, detach and sync for search or sync hooks.

### Documentation

#### [Installation](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/installation.md)

Install via Composer, publish and run the two publish-only migrations, and optionally publish the config file.

#### [Configuration](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/configuration.md)

Every config key and its default — model, table, owner key type, strict mode, pruning, owner deletion, history and attribute definitions.

#### [Database schema & model](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/database-schema.md)

The attributes and revisions tables, owner key types for UUID and ULID models, and swapping in your own Attribute model.

#### [The HasAttributes trait](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/has-attributes.md)

Add the HasAttributes trait and contract to any Eloquent model to give it typed, queryable attributes — plus the method reference.

#### [The Attributes facade](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/facade.md)

Attributes::for($owner) for every read and write of one model, plus definitions, validation, pruning and a recording fake.

#### [DI and actions](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/without-facade.md)

Skip the facade: inject AttributesManager for the same API without static calls, or call a single-purpose action directly.

#### [Typed values](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/typed-values.md)

Values keep their real PHP type — strings, integers, floats, booleans, arrays and datetimes round-trip without manual casting.

#### [Attaching attributes & metadata](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/attaching-attributes.md)

Attach one or many typed attributes, add metadata to any of them, and replace metadata without touching the value.

#### [The fluent builder](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/fluent-builder.md)

Stage several attributes with metadata, adjust the staged set, then save alongside existing attributes or sync to replace them.

#### [Reading attributes](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/reading-attributes.md)

Read typed values, strings, models and metadata; use the attr() accessor or typed readers; export attributes as key/value maps.

#### [Query scopes](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/query-scopes.md)

Filter and order owner models by their attributes — equality, sets, ranges, presence, null checks and ordering.

#### [Removing & syncing](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/removing-syncing.md)

Soft-delete or force-delete attributes, remove all but a keep-list, and sync a model’s attributes to an exact set.

#### [Definitions & validation](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/definitions.md)

Register known attributes with a type and Laravel validation rules, seed them from config, and opt into strict mode.

#### [Per-model schemas](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/per-model-schemas.md)

Declare attribute definitions on the model itself — as a property or a method — merged over global definitions for that model only.

#### [Unique, required & defaults](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/constraints-defaults.md)

Enforce unique values per model type or globally, assert required attributes on demand, and read defaults for unset attributes.

#### [Encrypted values](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/encryption.md)

Mark a definition encrypted to store its value as Crypt ciphertext at rest and decrypt it transparently on read — for every type.

#### [History & audit trail](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/history.md)

Opt in to an old→new revision on every attach, update, meta change and detach, then read it per model or per attribute, newest first.

#### [Events](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/events.md)

React to attribute changes with AttributeAttached, AttributeDetached and AttributesSynced — for audit logs, re-indexing or syncing.

#### [Artisan commands](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/commands.md)

List an owner’s attributes, prune old soft-deleted attributes, and inspect the package configuration with about.

#### [Enums](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/enums.md)

AttributeType, UniqueScope and RevisionType — with option lists, labels, lookups and comparators from enums-for-laravel.

#### [Exceptions](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/exceptions.md)

Every exception the package throws, when it is thrown and the context it carries — all extend AttributesException.

#### [Testing](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/testing.md)

Record writes with Attributes::fake() and its assertions, test against real rows with RefreshDatabase, and seed with the factory.

#### [Requirements](https://roundly-consulting.com/open-source/docs/attributes-for-laravel/requirements.md)

PHP 8.4+ and Laravel 12 or 13, plus three small Roundly packages that Composer installs automatically.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source.md)
