https://jsonapi.org/examples/#error-objects-basics
composer require sinema/json-api-error
<?php
use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\ErrorBag;
$errors = new ErrorBag();
$errors->add(
Error::fromArray(
[
'status' => 404,
'source' => null,
'title' => 'Item not found',
'detail' => sprintf('Item %s not found', 'some-id'),
]
)
);
$errors->toArray()
Result as JSON representation
[
{
"status": 404,
"title": "Item not found",
"detail": "Item some-id not found"
}
]
<?php
use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\Response;
$response = Response::get();
$response->add(
Error::fromArray(
[
'status' => 404,
'source' => null,
'title' => 'Item not found',
'detail' => sprintf('Item %s not found', 'some-id'),
]
)
);
$response->toArray()
Result as JSON representation
{
"errors": [
{
"status": 404,
"title": "Item not found",
"detail": "Item some-id not found"
}
]
}