spatie/schema-org
Fluent PHP builder for the full Schema.org vocabulary. Create Schema.org types and properties via chainable methods and output valid JSON-LD/ld+json scripts for SEO. Auto-generated from Schema.org standards for complete coverage.
LocalBusiness, Product, Person) and their properties, ensuring compliance with Google’s structured data requirements. This is critical for SEO, rich snippets, and knowledge graph integration.Schema::localBusiness()->name('Spatie')) aligns well with Laravel’s Eloquent and other fluent APIs, reducing cognitive load for developers familiar with Laravel conventions.Organization → Product → Offer) and multi-typed entities (e.g., HotelRoom + Product), which are common in e-commerce, travel, and local business use cases.application/ld+json for <script> tags or API responses, eliminating manual JSON-LD construction.Schema::macro() for custom types).@php echo Schema::localBusiness()->toScript() @endphp).return response()->json($schema->toArray())).Cache::remember()) since they’re immutable after construction.User → Person schema), enabling dynamic schema generation from DB records.created, updated) via Laravel’s observers or listeners.Spatie\ArrayToStream\ArrayToStream::stream()).->contactPoint(fn() => Schema::contactPoint())).bootstrap/cache (if using spatie/laravel-package-tools).toScript(): Fixed in v4.0.2 (escapes </> to \u003C/\u003E). Risk: Older versions (pre-4.0.2) could break scripts if user input contains </script>. Mitigation:
Str::of($input)->replaceMatched('/</script>/i', '')).Physician and Float: Excluded due to Schema.org health extension or PHP reserved keywords. Mitigation:
toScript() or toArray().Product + Service) or graphs (e.g., Organization → Person)?Product models)?LocalBusiness with color) be rejected or silently ignored?Spatie\SchemaOrg\Schema as a singleton or context-bound instance.@schema) for view integration.JsonResource to include schema metadata.symfony/property-access for dynamic property handling (if extending the package).nette/utils for string manipulation (e.g., escaping).JSON-LD library (e.g., schema-org-js) to validate client-side.Offer for products, BreadcrumbList for navigation).Schema::product()->name($product->name).Organization for homepage).Article for blog posts, tied to Eloquent models).LocalBusiness → Menu → Dish).spatie/laravel-testing to assert schema output.json, mbstring (for multibyte escaping in toScript()).User → Person).Cache::forever('schema:product:123', $schema->toArray())).composer require spatie/schema-org.php artisan vendor:publish --provider="Spatie\SchemaOrg\SchemaOrgServiceProvider".LocalBusiness, Article).// app/Http/Controllers/ProductController.php
public function show(Product $product) {
$schema = Schema::product()
->name($product->name)
->description($product->description)
->offers(Schema::offer()
->price($product->price)
->currency('USD')
);
return view('product', ['schema' => $schema->toScript()]);
}
ProductSchema trait).// app/Models/Product.php
use Spatie\SchemaOrg\Product as SchemaProduct;
class Product extends Model {
public function toSchema(): SchemaProduct {
return Schema::product()
->name($this->name)
->image($this->imageUrl)
->offers(Schema::offer()->price($this->price));
}
}
How can I help you explore Laravel packages today?