A fluent builder types and ld+json generator

3.23.0 2024-01-11 14:58 UTC



A Fluent Builder For Types And ld+json Generator

Latest Version on Packagist MIT License GitHub Workflow Status StyleCI Total Downloads

spatie/schema-org provides a fluent builder for all types and their properties. The code in src is generated from's JSON-LD standards file, so it provides objects and methods for the entire core vocabulary. The classes and methods are also fully documented as a quick reference.

use Spatie\SchemaOrg\Schema;

$localBusiness = Schema::localBusiness()

echo $localBusiness->toScript();
<script type="application/ld+json">
    "@context": "https:\/\/",
    "@type": "LocalBusiness",
    "name": "Spatie",
    "email": "",
    "contactPoint": {
        "@type": "ContactPoint",
        "areaServed": "Worldwide"

Support us


We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.


You can install the package via composer:

composer require spatie/schema-org


All types can be instantiated through the Spatie\SchemaOrg\Schema factory class, or with the new keyword.

$localBusiness = Schema::localBusiness()->name('Spatie');

// Is equivalent to:

$localBusiness = new LocalBusiness();

All types also accept arrays of the expected data type, for example sameAs accepts a string or an array of strings.

All types also implement the SPL's ArrayAccess for accessing the properties via array notation:

$anotherLocalBusiness = new LocalBusiness();
var_dump(isset($anotherLocalBusiness['name'])); // => false
$anotherLocalBusiness['name'] = 'Spatie';
var_dump(isset($anotherLocalBusiness['name'])); // => true
var_dump($anotherLocalBusiness['name']); // => 'Spatie'
var_dump(isset($anotherLocalBusiness['name'])); // => false

Types can be converted to an array or rendered to a script.


echo $localBusiness->toScript();

echo $localBusiness; // Same output as `toScript()`

Additionally, all types can be converted to a plain JSON string by just calling json_encode() with your object:

echo json_encode($localBusiness);

I recommend double checking your structured data with Google's structured data testing tool.


As of v1.6.0, all Enumeration child types are available as classes with constants.


There's no full API documentation for types and properties. You can refer to the source or to the website.

If you don't want to break the chain of a large schema object, you can use the if method to conditionally modify the schema.

use Spatie\SchemaOrg\LocalBusiness;
use Spatie\SchemaOrg\Schema;

$business = ['name' => 'Spatie'];

$localBusiness = Schema::localBusiness()
    ->if(isset($business['email']), function (LocalBusiness $schema) use ($business) {


As of v2.6.0 the identifier key is replaced by @id for simple string identifiers. This is due to the definition for the ld+json syntax.

All syntaxes already have built-in representation for URIs and URLs, e.g. in Microdata 'itemid', in RDFa 1.1, 'resource', in JSON-LD, '@id'. // PR#102 // PR#157

Advanced Usage

If you'd need to set a custom property, you can use the setProperty method.

$localBusiness->setProperty('foo', 'bar');

If you'd need to retrieve a property, you can use the getProperty method. You can optionally pass in a second parameter to provide a default value.

$localBusiness->getProperty('name'); // 'Spatie'
$localBusiness->getProperty('bar'); // null
$localBusiness->getProperty('bar', 'baz'); // 'baz'

All properties can be retrieved as an array with the getProperties method.

$localBusiness->getProperties(); // ['name' => 'Spatie', ...]

Multiple properties can be set at once using the addProperties method.

$localBusiness->addProperties(['name' => 'value', 'foo' => 'bar']);

Context and type can be retrieved with the getContext and getType methods.

$localBusiness->getContext(); // ''
$localBusiness->getType(); // 'LocalBusiness'

Graph - multiple items

The Graph has a lot of methods and utilities - the type-safe and simplest way is to use the overloaded methods of the Spatie\SchemaOrg\Schema class itself. These methods will get an already created or new instance of the requested schema.

$graph = new Graph();

// Create a product and prelink organization
    ->name('My cool Product')

// Hide the organization from the created script tag

// Somewhere else fill out the organization
    ->name('My awesome Company');

// Render graph to script tag
echo $graph;

With these tools the graph is a collection of all available schemas, can link these schemas with each other and prevent helper schemas from being rendered in the script-tag.

Graph Node identifiers

Sometimes you have to keep track of multiple Graph nodes of the same type - for example multiple Person nodes for different people in your Organization. To do so you are able to use node identifiers on your graph instance. If you don't provide an identifier a reserved keyword default identifier will be used.

use Spatie\SchemaOrg\Graph;
use Spatie\SchemaOrg\Person;

$graph = new Graph();

// add a Person using chaining
    ->familyName('Van der Herten')

// add a Person using closure
$graph->person('sebastiandedeyne', function(Person $sebastian, Graph $graph): void {
        ->familyName('De Deyne')

// add a person using closure and second call with same identifier
    fn(Person $gummibeer) => $gummibeer->alternateName('gummibeer')

$graph->person('random')->name('Random Person');

// hide the random person from Graph
$graph->hide(Person::class, 'random');

echo json_encode($graph);
            "familyName":"Van der Herten",
            "familyName":"De Deyne",

Multi Typed Entities allows multi typed entities - to use them with this package you can use the MultiTypedEntity class - which works similar to the graph.

$mte = new MultiTypedEntity();
$mte->hotelRoom()->name('The Presidential Suite');
        ->name('One Night')
$mte->product(function (Product $product) {

echo json_encode($mte);
   "name":"The Presidential Suite",
      "name":"One Night",

There isn't a real rule in place how the properties are merged. It only uses array_merge() behind the scenes. So you should avoid defining the same property on different types in the MTE or be sure that all properties hold the same value that it's not important which property is used at the end.

Known Issues

  • The Float type isn't available since it's a reserved keyword in PHP
  • The Physician type isn't available since it extends a type from the health extension spec


Please see CHANGELOG for more information what has changed recently.


$ composer test


Please see CONTRIBUTING for details.


If you've found a bug regarding security please mail instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.



The MIT License (MIT). Please see License File for more information.