neomerx/json-api
Framework-agnostic PHP library implementing JSON:API v1.1. Builds compliant documents, relationships, compound includes, meta and errors. Parses/validates Accept/Content-Type and query params (pagination, sorting, sparse fields) to return proper 415/406 responses.
Schema classes (e.g., AuthorSchema), mirroring Laravel’s Eloquent model structure.Content-Type, Accept headers, and query parameter parsing (e.g., pagination, filtering), reducing boilerplate in Laravel middleware/controllers.Schema methods).array_column).Content-Type/Accept headers and parse query params (e.g., include, fields).AppServiceProvider for global access to Encoder, Decoder, and ErrorHandler.{resource} → Encoder::encodeData($resource)).Schema classes (e.g., getAttributes() → model->toArray()). No ORM-specific optimizations.hasMany, belongsTo, etc., but requires explicit configuration in getRelationships() (e.g., self::RELATIONSHIP_DATA => $author->comments).SchemaInterface::getType(): string may conflict with PHP 8.1’s stricter type system).json_encode without JSON_THROW_ON_ERROR flag).ContextInterface) adds minor overhead but is negligible for most use cases.include, sort) is efficient but may slow down complex APIs with deep nesting.ErrorCollection provides helpers).return_type_declaration polyfills for PHP 7.1–7.4 if using PHP 8.1+.Schema configuration.ContextInterface to inject auth data (e.g., user ID).Error class or Laravel’s HttpResponse exceptions? Plan for consistency.spatie/laravel-fractal or nesbot/json-api-php./api/v1/posts vs. /api/v2/posts).Response::json() with Encoder::encodeData() for standardized responses.Decoder::decodeData($request->getContent())).Content-Type checks with the package’s MediaTypeParser.JsonApiMiddleware to:
Accept: application/vnd.api+json.include, fields, sort).ContextInterface with request data (e.g., auth, URL prefix).Schema classes to lazy-load relationships (e.g., getRelationships() uses with()).Schema attributes.vue-jsonapi or ember-data for seamless consumption.Response::json() with Encoder::encodeData() for new endpoints.// Before
return response()->json($post->toArray());
// After
return response()->json(Encoder::encodeData($post));
php artisan make:schema PostSchema (custom command) to generate Schema classes.JsonApiMiddleware to validate incoming requests.Decoder::decodeData() for PATCH/PUT requests.$data = Decoder::decodeData($request->getContent());
$post = Post::find($data['id']);
$post->update($data['attributes']);
X-API-Version headers.| Laravel Version | PHP Version | Compatibility Notes |
|---|---|---|
| 6.x | 7.1–7.4 | Fully compatible. |
| 7.x | 7.2–7.4 | Fully compatible. |
| 8.x | 7.3–8.0 | Test for PHP 8.0+ (e.g., union types). |
| 9.x | 8.0+ | High risk; may need Symfony HTTP adapters. |
json, mbstring).composer why-not neomerx/json-api).How can I help you explore Laravel packages today?