Custom Fields
Custom fields attach structured data to content — a subtitle, a price, a cover image. Laravix supports two ways to define them: in code (portable, versioned) and in the admin (quick, per site). This article covers both plus the field type reference.
Field types
Both definition styles share the same types (Laravix\Cms\Enums\FieldType):
| Type | Admin component | Stored value |
|---|---|---|
text |
Text input | string |
textarea |
Multi-line text | string |
rich_text |
Rich text editor | HTML string |
markdown |
Markdown editor | Markdown string |
image / file |
Media picker (choose from library or upload) | media ID |
boolean |
Toggle | 0/1 |
select |
Select (options from config(['options' => [...]])) |
option key |
url |
URL input | string |
date / datetime |
Date / date-time picker | date string |
number |
Numeric input | numeric string |
color |
Color picker | hex string |
Values are stored as key–value rows in content_fields and surface in the content form grouped into sections by their group.
Option 1: fields in code
Registered in a service provider's register(); ideal for plugins and anything you want in version control:
use Laravix\Cms\Enums\FieldType;
use Laravix\Cms\Support\FieldDefinition;
use Laravix\Cms\Support\FieldRegistry;
// for every content type:
FieldRegistry::content([
FieldDefinition::make('subtitle')->label('Subtitle'),
]);
// for one content type:
FieldRegistry::contentType('post', [
FieldDefinition::make('excerpt')
->type(FieldType::TEXTAREA)
->label('Excerpt')
->group('Post details')
->hint('Shown in post listings.'),
]);
FieldDefinition methods: type(), label(), group() (form section heading), hint() (helper text), config() (extras like ['options' => [...]] for selects).
The core itself registers the SEO fields this way (meta_title, meta_description, og_image, noindex) — they're just fields in the SEO group.
Option 2: fields in the admin
Site admins can define fields without code:
- Open Custom fields in the admin menu.
- Create a field: Content type, Type, Key (immutable after creation), Label, optional Group and Hint, Sort order, Required toggle.
- The field appears on the content form of that type immediately.
Admin-defined fields are per site — perfect for site-specific data. Definitions are cached for an hour but the cache is cleared automatically when you save or delete a field.
Note: If an admin-defined field uses the same key as a code-defined one, the code definition wins. And on headless sites, the content form shows only admin-defined custom fields — the code-registered SEO fields are hidden, since SEO is the head's job.
Reading fields
In themes:
@php $fields = $content->fields->pluck('value', 'key'); @endphp
<p>{{ $fields->get('excerpt') }}</p>
For image/file fields the value is a media ID — resolve it via $mediaMap[(int) $fields->get('cover')] ?? null (the media map already contains every media referenced by registered image fields).
In the API: every content response carries a fields object with the raw key–value pairs, so a headless frontend gets your custom data automatically. See API Reference.