Installation Add the package via Composer:
composer require laravel-json-api/encoder-neomerx
Ensure neomerx/json-api is also installed (this package depends on it).
Basic Usage
Register the encoder in your config/json-api.php under the encoders key:
'encoders' => [
\LaravelJsonApi\Encoder\NeomerxEncoder::class,
],
Then, encode a resource in a controller or service:
use LaravelJsonApi\Encoder\NeomerxEncoder;
$encoder = app(NeomerxEncoder::class);
$resource = new \Neomerx\JsonApi\Resource($data, $type, $id);
$json = $encoder->encode($resource);
First Use Case Use this encoder when you need strict JSON:API compliance (e.g., for APIs consumed by third-party clients expecting exact spec adherence). Ideal for:
neomerx/json-api is already in use.Resource Transformation Use the encoder to transform Eloquent models or collections into JSON:API-compliant responses:
$users = User::all();
$resources = \LaravelJsonApi\Encoder\NeomerxEncoder::encodeCollection(
$users,
new \Neomerx\JsonApi\ResourceCollection(
$users->map(fn ($user) => new \Neomerx\JsonApi\Resource($user, 'users', $user->id))
)
);
Customizing Resource Objects
Extend \Neomerx\JsonApi\Resource to add custom attributes or relationships:
class CustomUserResource extends \Neomerx\JsonApi\Resource
{
public function getAttributes()
{
return parent::getAttributes() + ['custom_field' => $this->customField];
}
}
Integration with Laravel JSON:API
If using laravel-json-api/laravel, pair this encoder with the framework’s serializers for a unified approach:
$serializer = app(\LaravelJsonApi\Serializers\Serializer::class);
$serializer->setEncoder(app(NeomerxEncoder::class));
try {
$json = $encoder->encode($resource);
} catch (\Neomerx\JsonApi\Exceptions\InvalidResourceException $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
neomerx/json-api's pagination helpers (e.g., Page) for consistent pagination formats.Meta object:
$resource->setMeta(['custom' => 'value']);
Strict JSON:API Compliance
The encoder enforces the JSON:API spec rigorously. Non-compliant data (e.g., missing id or type) will throw exceptions. Validate resources before encoding:
$resource->validate();
Performance Overhead For large datasets, encoding collections can be resource-intensive. Optimize by:
->cursor()).Dependency Conflicts
Ensure neomerx/json-api is updated to a compatible version (check packagist). Conflicts may arise with older Laravel versions.
JSON_API_DEBUG in .env to log encoder issues.dd($resource) to verify data structure before encoding.neomerx/json-api releases for breaking changes.Custom Encoders
Extend \LaravelJsonApi\Encoder\NeomerxEncoder to modify behavior:
class CustomNeomerxEncoder extends NeomerxEncoder
{
public function encode($resource)
{
// Custom logic
return parent::encode($resource);
}
}
Middleware for Validation Add middleware to validate JSON:API requests before encoding:
use Neomerx\JsonApi\Exceptions\InvalidDocumentException;
public function handle($request, Closure $next)
{
try {
$document = \Neomerx\JsonApi\Document::fromArray($request->all());
$document->validate();
} catch (InvalidDocumentException $e) {
return response()->json(['error' => $e->getMessage()], 422);
}
return $next($request);
}
Testing Mock the encoder in tests to isolate logic:
$encoder = Mockery::mock(NeomerxEncoder::class);
$encoder->shouldReceive('encode')->andReturn('{"data":{...}}');
How can I help you explore Laravel packages today?