Overview

How the traits, models, DTOs, and services fit together

Usage Overview

The package revolves around three polymorphic models attached to a host model:

  • Metadata — standard SEO meta tags, exposed via the HasMetadata trait's metadata() relation.
  • OpenGraph — social sharing data, exposed via the HasOpenGraph trait's openGraph() relation.
  • JsonLd — Schema.org structured data, exposed via the HasJsonLd trait's jsonLd() relation.

All are morphOne relations: a host model has at most one of each. The single JSON-LD record holds a list of graphs, so a page can still expose several structured-data objects.

Opt a model in

Add a trait to any Eloquent model:

php
use Illuminate\Database\Eloquent\Model;
use OiLab\OiLaravelMetadata\Concerns\HasMeta;

class Page extends Model
{
    use HasMeta; // both relations — or HasMetadata / HasOpenGraph individually
}

You now have the relations:

php
$page->metadata;   // Metadata|null
$page->openGraph;  // OpenGraph|null
$page->jsonLd;     // JsonLd|null

The moving parts

PieceRole
HasMetadata / HasOpenGraph / HasJsonLd / HasMetaTraits that add the polymorphic relations and helpers
MetadataData / OpenGraphData / OpenGraphImageData / JsonLdDataTyped DTOs (spatie/laravel-data) you read and write
SchemaFluent Schema.org node builder for JSON-LD
MetaService / OgService / JsonLdServiceServices that read, write (updateOrCreate), and render
Meta / Og / JsonLdFacades over the three services
@meta / @og / @jsonLdBlade directives that render a source (optional — see below)
Seo / SeoContextShared "current subject" so the directives render with no argument
OiMetadataResolver for the configurable model classes

Where to go next

Project under MIT License.
Design by