Skip to main content

Laravel Metadata Showcase

Discover real-world examples and use cases of Laravel Metadata in action. See how different applications leverage dynamic metadata to store custom fields, user preferences, product attributes, and extensible data without modifying database schemas.

Understanding Metadata Components

Laravel Metadata provides powerful features for managing dynamic data:

  • HasMeta Trait: Core trait that adds metadata functionality to any Eloquent model
  • Polymorphic Storage: Single table for metadata across all model types
  • Key Whitelisting: Control which keys are allowed per model
  • Automatic JSON Handling: Arrays and objects are automatically encoded/decoded
  • Query Support: Filter models by metadata keys and values
  • Event Integration: Hook into metadata lifecycle for custom logic

E-Commerce Product Attributes

Dynamic Product Attributes

Store category-specific product attributes without schema changes

Overview

E-commerce platforms often need to store different attributes for different product categories. Clothing products need size and color, while electronics need brand and model. Laravel Metadata enables you to store these category-specific attributes dynamically without modifying your database schema or creating separate tables for each category.

Product Attributes
  • Color, size, material
  • Brand, model, warranty
  • Dimensions, weight
  • Specifications
  • Custom fields
  • Category-specific data
Business Benefits
  • Flexible attribute management
  • No schema changes needed
  • Category-specific attributes
  • Easy filtering and search
  • Scalable architecture
  • Better product organization

Implementation Example

namespace App\Models;

use JobMetric\Metadata\HasMeta;

class Product extends Model
{
use HasMeta;

protected array $metadata = [
'color',
'size',
'weight',
'dimensions',
'material',
'brand',
'model',
'warranty_period',
];

protected $fillable = ['name', 'price', 'category_id'];
}

// Store product attributes
$product = Product::create([
'name' => 'Cotton T-Shirt',
'price' => 29.99,
'category_id' => 1,
]);

$product->storeMetadataBatch([
'color' => 'blue',
'size' => 'large',
'material' => 'cotton',
'weight' => 200,
'dimensions' => [
'width' => 50,
'height' => 70,
],
]);

// Query products by attributes
$blueProducts = Product::hasMetaKey('color')
->whereHas('metas', function ($query) {
$query->where('key', 'color')
->where('value', 'blue');
})
->get();

// Get product attributes
$color = $product->getMetadata('color');
$dimensions = $product->getMetadata('dimensions');

User Preferences and Settings

User Preferences Management

Store user preferences and settings dynamically

Overview

Applications often need to store user preferences, settings, and custom data that doesn't fit into the main user table. Laravel Metadata makes it easy to store these preferences dynamically, allowing users to customize their experience without requiring schema changes.

User Preferences
  • Theme preferences
  • Notification settings
  • Language preferences
  • Display options
  • Custom settings
  • Feature flags
Business Benefits
  • Personalized experiences
  • Flexible settings
  • No schema changes
  • Easy to extend
  • Better user experience
  • Scalable preferences

Implementation Example

namespace App\Models;

use JobMetric\Metadata\HasMeta;

class User extends Model
{
use HasMeta;
// No $metadata array = all keys allowed
}

// Store user preferences
$user = User::find(1);
$user->storeMetadataBatch([
'theme' => 'dark',
'language' => 'en',
'notifications' => [
'email' => true,
'sms' => false,
'push' => true,
],
'display_options' => [
'items_per_page' => 20,
'show_avatars' => true,
],
]);

// Get preferences with defaults
$theme = $user->getMetadata('theme', 'light');
$notifications = $user->getMetadata('notifications', [
'email' => true,
'sms' => true,
'push' => true,
]);

// Update single preference
$user->storeMetadata('theme', 'light');

// Check if preference exists
if ($user->hasMetadata('theme')) {
$theme = $user->getMetadata('theme');
}

Content Management with Metadata

SEO and Content Metadata

Store SEO metadata and content attributes

Overview

Content management systems often need to store SEO metadata, custom fields, and additional attributes for posts, pages, and articles. Laravel Metadata provides a flexible solution for storing this data without cluttering the main content table.

Content Metadata
  • SEO title and description
  • Meta keywords
  • Open Graph data
  • Custom fields
  • Featured image
  • Content attributes
Business Benefits
  • Better SEO control
  • Flexible content structure
  • Easy metadata management
  • Social media integration
  • Custom field support
  • Clean content tables

Implementation Example

namespace App\Models;

use JobMetric\Metadata\HasMeta;

class Post extends Model
{
use HasMeta;

protected array $metadata = [
'seo_title',
'seo_description',
'seo_keywords',
'og_image',
'og_title',
'og_description',
'featured_image',
'reading_time',
];
}

// Store SEO metadata
$post = Post::create([
'title' => 'My Blog Post',
'content' => '...',
]);

$post->storeMetadataBatch([
'seo_title' => 'My Blog Post - SEO Title',
'seo_description' => 'This is the SEO description',
'seo_keywords' => ['laravel', 'php', 'tutorial'],
'og_image' => '/images/og-image.jpg',
'og_title' => 'My Blog Post',
'og_description' => 'Open Graph description',
'reading_time' => 5,
]);

// Get SEO data for views
$seoTitle = $post->getMetadata('seo_title', $post->title);
$seoDescription = $post->getMetadata('seo_description', '');