Metadata
Kompass provides a flexible system for storing arbitrary key-value data on any Eloquent model via the HasMeta trait — no per-model schema changes required.
Looking for SEO tags?
For managing <title>, <meta>, and Open Graph tags, see the dedicated SEO feature.
HasMeta Trait
The HasMeta trait lets you store and retrieve custom metadata on any model. Values can be strings, arrays, or objects — arrays are stored as JSON automatically.
Setup
Add the trait to your model:
php
use Secondnetwork\Kompass\Traits\HasMeta;
class Page extends Model
{
use HasMeta;
}Usage
php
// Set a single value (persists immediately)
$page->setMeta('author', 'John Doe');
// Set multiple values at once
$page->saveMeta(['title' => 'My Page', 'keywords' => 'laravel, cms']);
// Read a value, with an optional fallback
$page->getMeta('author');
$page->getMeta('author', 'Default Author');
// Remove a value
$page->deleteMeta('author');| Method | Description |
|---|---|
getMeta($key, $default = null) | Retrieve a value, with an optional fallback. |
setMeta($key, $value) | Set and persist a single value. |
saveMeta(array $data) | Set and persist multiple key-value pairs. |
deleteMeta($key) | Remove a stored value. |
Database Schema
Kompass stores all metadata in a single polymorphic meta table, shared across every model that uses the trait.
| Column | Type | Description |
|---|---|---|
id | bigint | Primary key |
metable_id | bigint | Related model ID |
metable_type | string | Related model class |
key | string | Meta key |
value | text | Meta value (JSON for arrays) |