- How do I generate JSON-LD for a Laravel product page using Schema.org types?
- Use the fluent builder to chain properties: `Schema::product()->name($product->name)->image($product->imageUrl)->offers(Schema::offer()->price($product->price))`, then output with `$productSchema->toScript()`. For dynamic data, cache the result with Laravel’s cache system or generate it asynchronously via queues.
- Which Laravel versions does spatie/schema-org support?
- The package supports Laravel 8.x, 9.x, and 10.x. Check the [Packagist page](https://packagist.org/packages/spatie/schema-org) for the latest version and its PHP 8.0+ requirements. Downgrades below Laravel 8 may require manual adjustments due to dependency changes.
- Can I use this package to generate structured data for blog articles?
- Yes. Start with `Schema::article()->headline($post->title)->datePublished($post->publishedAt)->author(Schema::person()->name($author->name))`. For rich snippets, include `publisher` or `provider` properties to link to your organization. Test with Google’s [Rich Results Test](https://search.google.com/test/rich-results) to validate.
- How do I integrate Schema.org types into Eloquent models?
- Use Eloquent casts to convert model attributes to Schema.org types. For example, add `protected $casts = ['schema_type' => SchemaCast::class]` to your model, then define a `getSchema()` method returning the generated schema. Alternatively, use model observers to auto-generate schemas on save events.
- Does this package support nested or multi-type schemas (e.g., a Product with Reviews and Ratings)?
- Yes. Use the `Graph` class to manage interconnected entities. For example, `$graph = Schema::graph()->add(Schema::product()->name('...'))->add(Schema::aggregateRating()->ratingValue(4.5));` This is ideal for complex schemas like e-commerce products with reviews or local businesses with events.
- How do I handle Schema.org updates (e.g., new types or deprecated properties)?
- The package auto-generates classes from Schema.org’s JSON-LD standards file, so updates are handled via package releases. Monitor [Schema.org releases](https://schema.org/docs/releases.html) and test new features in a staging environment. Use feature flags or config settings to toggle support for experimental types.
- Can I use this package in API responses (e.g., GraphQL or REST endpoints)?
- Yes. Serialize the schema to an array with `$schema->toArray()` and include it in your API response. For GraphQL, return it as a JSON string or embed it directly in the response payload. Ensure your API clients handle JSON-LD properly, especially for nested or large schemas.
- What’s the best way to cache generated schemas in Laravel?
- Use Laravel’s cache system with `Cache::remember()`. For example: `Cache::remember('product-schema-' . $product->id, now()->addHour(), function() use ($product) { return Schema::product()->name($product->name)->generate(); });`. For high-traffic sites, consider edge caching (e.g., Cloudflare) or async generation via Laravel queues.
- Are there alternatives to spatie/schema-org for Laravel?
- Alternatives include manual JSON-LD generation, libraries like `digitalbazaar/json-ld` (lower-level), or `thephpleague/html` for Microdata/RDFa. However, spatie/schema-org stands out for its Laravel-native fluent builder, auto-generated type safety, and seamless integration with Blade, Eloquent, and APIs—reducing boilerplate significantly.
- How do I validate my generated JSON-LD before deploying to production?
- Use Google’s [Rich Results Test](https://search.google.com/test/rich-results) for SEO validation. For broader compliance, integrate tools like [Schema Markup Validator](https://validator.schema.org/) or write unit tests with assertions against expected JSON-LD structures. Test edge cases like empty arrays, special characters, and nested properties.