Skip to main content
Attributes store custom, translatable data against Eloquent models using configurable field types.

Overview

Attributes allow custom data to be stored against Eloquent models. They are most commonly used with products, where different information needs to be stored and presented to visitors. For example, a television might have the following attributes assigned:
  • Screen Size
  • Screen Technology
  • Tuner
  • Resolution
Attributes are organized into Attribute Groups for display purposes. A group like “SEO” might contain attributes for “Meta Title” and “Meta Description”.

Attribute Groups

Attribute groups form a logical collection of attributes. Each group belongs to a specific model type (e.g. products, collections, customers).

Fields

Relationships

Attributes

Fields

Relationships

Scopes

Field Types

Field types determine how an attribute’s value is stored and retrieved. Each type implements the Lunar\Base\FieldType interface.

Custom Field Types

Custom field types can be created by implementing the Lunar\Base\FieldType interface. Once created, the field type should be registered with the FieldTypeManifest in a service provider:
To make a custom field type editable in the admin panel, a corresponding Filament component and Livewire synthesizer are also needed. See Extending Attributes for a full walkthrough.

Models That Use Attributes

The following models support attributes out of the box:
  • Lunar\Models\Product
  • Lunar\Models\ProductVariant
  • Lunar\Models\Collection
  • Lunar\Models\Customer
  • Lunar\Models\Brand
  • Lunar\Models\CustomerGroup

Saving Attribute Data

Attribute values are stored in an attribute_data JSON column on the model. Each key is an attribute handle, and each value is a field type instance.

Accessing Attribute Data

When the attribute_data property is accessed, it is cast to a collection of field type instances.

Retrieving a Single Attribute Value

The translateAttribute method returns the resolved value for a single attribute. For TranslatedText fields, it resolves the correct locale automatically.
The shorthand attr method does the same thing:
For non-translatable fields, translateAttribute returns the raw value directly:

Adding Attributes to a Custom Model

To make a custom model support attributes:
  1. Add the HasAttributes trait.
  2. Cast the attribute_data column using AsAttributeData.
  3. Add an attribute_data JSON column to the model’s database table.
Then add the JSON column via a migration:
Finally, register the model as an attributable type so that attribute groups and attributes can be created for it:

Attribute Manifest

The AttributeManifest manages which model types support attributes and provides access to searchable attribute data.