Installation
composer require effectiveactivism/schema-org-api
Add the service provider to config/app.php under providers:
EffectiveActivism\SchemaOrg\Providers\SchemaOrgServiceProvider::class,
First Use Case: Querying Schema.org Types
Use the SchemaOrg facade to fetch a type definition:
use EffectiveActivism\SchemaOrg\Facades\SchemaOrg;
$personType = SchemaOrg::type('Person');
dd($personType->properties); // Inspect properties
Explore the GraphQL Schema The package exposes Schema.org as a GraphQL schema. Start with the root query:
$query = SchemaOrg::query()
->type('Thing')
->properties()
->get();
Type Introspection Dynamically inspect Schema.org types and their relationships:
$event = SchemaOrg::type('Event');
$event->subTypes(); // ['Reservable', 'OfferCatalog', ...]
$event->superType(); // 'Thing'
Property Validation Validate input data against Schema.org constraints:
$validator = SchemaOrg::validator('Person');
$validator->validate([
'name' => 'John Doe',
'age' => 30, // Will fail (age is not a valid property for Person)
]);
GraphQL Query Building Construct queries for specific use cases (e.g., SEO metadata):
$query = SchemaOrg::query()
->type('WebPage')
->properties(['name', 'description', 'publisher'])
->get();
Integration with Laravel Models Attach Schema.org types to Eloquent models:
use EffectiveActivism\SchemaOrg\Traits\HasSchemaOrgType;
class BlogPost extends Model
{
use HasSchemaOrgType;
protected $schemaOrgType = 'BlogPosting';
}
$type = SchemaOrg::remember('Person', 60, function () {
return SchemaOrg::type('Person');
});
$jsonLd = SchemaOrg::jsonLd($model, 'BlogPosting');
view()->share('jsonLd', $jsonLd);
return response()->json([
'data' => $validator->validate($request->all()),
]);
Type Hierarchy Confusion
Person extends Thing). Always check superType() and subTypes() to avoid missing inherited properties.SchemaOrg::type('Thing')->properties() to get all possible properties for a type hierarchy.Property Constraints
datePublished must be a DateTime). The validator will throw exceptions for mismatches.SchemaOrg::type('Event')->property('datePublished')->type() to check constraints before validation.GraphQL Schema Limitations
SchemaOrg::query()->explain().Performance with Large Datasets
SchemaOrg::type($name, ['load' => 'properties']) to limit loaded data.$type = SchemaOrg::type('Thing');
dd($type->toArray()); // Raw data structure
SchemaOrg::enableQueryLogging();
SchemaOrg::query()->type('Event')->properties()->get();
// Check logs for generated GraphQL queries.
Custom Type Mappings Override default type definitions in a service provider:
SchemaOrg::extend('CustomType', function () {
return SchemaOrg::type('Thing')->mergeProperties([
'customField' => ['type' => 'string', 'description' => 'Custom field'],
]);
});
Add Custom Validators Extend validation logic for specific use cases:
SchemaOrg::validator('Person')->extend('age', function ($attribute, $value, $fail) {
if ($value < 18) {
$fail('Age must be 18 or older.');
}
});
Hook into JSON-LD Generation Modify JSON-LD output globally:
SchemaOrg::macro('jsonLd', function ($model, $type) {
$jsonLd = parent::jsonLd($model, $type);
$jsonLd['@context'] = 'https://schema.org';
return $jsonLd;
});
http://schema.org by default. Override in config/schema-org.php:
'context' => 'https://custom-schema.org',
'graphql' => [
'max_depth' => 10,
],
How can I help you explore Laravel packages today?