Skip to main content

Manage Metadata. Simply and Flexibly.

Laravel Metadata is a powerful package that provides a robust and flexible metadata system for Laravel Eloquent models. It enables you to attach dynamic key-value data to any model through a polymorphic relationship, perfect for storing custom fields, settings, options, and user-defined attributes without modifying your database schema.

Why Laravel Metadata?

Dynamic Key-Value Storage

Laravel Metadata allows you to store arbitrary key-value pairs for any Eloquent model without modifying the model's table structure. This is perfect for storing custom fields, user preferences, product attributes, and any other extensible data.

Polymorphic Relationships

The package uses Laravel's polymorphic relationships, allowing a single metas table to store metadata for multiple model types. This keeps your database schema clean and maintainable while providing maximum flexibility.

Key Whitelisting

Control which metadata keys are allowed for each model. Define a whitelist of allowed keys to prevent typos and ensure data integrity, or use ['*'] to allow all keys.

Automatic JSON Handling

Arrays and objects are automatically JSON-encoded when stored and decoded when retrieved. The package tracks which values are JSON, ensuring proper type handling.

Event System

Laravel Metadata integrates with Laravel Event System, firing events when metadata is stored or deleted. Listen to these events to implement custom logic, logging, or notifications.

Query Scopes

Use query scopes to filter models based on their metadata. Find all models with a specific metadata key or value, making it easy to build dynamic filtering systems.

What is Metadata Management?

Metadata management is the process of storing and retrieving additional information about your models that doesn't fit into the main table structure. Traditional approaches often require:

  • Adding columns to tables (inflexible)
  • Creating separate tables for each model (redundant)
  • Using JSON columns (limited querying)

Laravel Metadata solves these challenges by providing:

  • Polymorphic Storage: One table for all metadata across all models
  • Dynamic Keys: Add new metadata keys without schema changes
  • Type Safety: Automatic JSON encoding/decoding
  • Query Support: Filter and search by metadata
  • Key Control: Whitelist allowed keys per model

Key Features

HasMeta Trait

The core trait that adds metadata functionality to any Eloquent model. Provides methods for storing, retrieving, and managing metadata.

HasFilterMeta Trait

Adds dynamic filtering capabilities based on metadata. Perfect for building admin panels and APIs that need to filter models by metadata values.

HasDynamicMeta Trait

Integrates with custom field systems to automatically manage allowed metadata keys based on model types.

Batch Operations

Store or delete multiple metadata entries in a single operation, improving performance for bulk updates.

Model Events

Metadata operations trigger Laravel events, allowing you to hook into the metadata lifecycle for custom logic.

Quick Example

Here's a quick example of how easy it is to work with metadata:

use JobMetric\Metadata\HasMeta;

class Product extends Model
{
use HasMeta;

protected array $metadata = [
'color',
'size',
'weight',
'dimensions',
];
}

// Store metadata
$product = Product::find(1);
$product->storeMetadata('color', 'red');
$product->storeMetadata('dimensions', ['width' => 10, 'height' => 20]);

// Retrieve metadata
$color = $product->getMetadata('color');
$dimensions = $product->getMetadata('dimensions');

// Store multiple at once
$product->storeMetadataBatch([
'color' => 'blue',
'size' => 'large',
'weight' => 500,
]);

// Query by metadata
$redProducts = Product::hasMetaKey('color')->get();

What Awaits You?

In this documentation, you'll learn:

  • Installation: How to install and configure the package
  • Basic Usage: Working with metadata on models
  • Advanced Features: Filtering, dynamic metadata, and batch operations
  • API Reference: Complete method documentation
  • Best Practices: Patterns and common use cases

Ready to get started? Let's install the package and begin managing metadata with confidence!