sinema/json-api-error-laravel

Format generic JSON API errors for Laravel
4,220 1
Install
composer require sinema/json-api-error-laravel
Latest Version:0.6
PHP:^8.2.0|^8.3.0|^8.4.0|^8.5.0
License:MIT
Last Updated:Jul 7, 2026
Links: GitHub  ·  Packagist
Maintainer: SineMah

JSON:API error response

Spec

https://jsonapi.org/examples/#error-objects-basics

Installation

composer require sinema/json-api-error-laravel

Usage

In your Laravel controller:

<?php

namespace App\Http\Controllers;

use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\Responses\Laravel;
use Illuminate\Http\JsonResponse;

class AnyController extends Controller
{
    public function show(): JsonResponse
    {
        return Laravel::get()->json(
            Error::fromArray(
                [
                    'status' => 404,
                    'source' => null,
                    'title' => 'Item not found',
                    'detail' => sprintf('Item %s not found', request('item_uuid')),
                ]
            ),
            404
        );
    }
}

Response

{
    "errors": [
        {
            "status": 404,
            "title": "Item not found",
            "detail": "Item bd11f048-8663-4d95-8c7a-02a5579b0682 not found in customer data"
        }
    ]
}

Build an error stack.

<?php

namespace App\Http\Controllers;

use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\Responses\Laravel;
use Illuminate\Http\JsonResponse;

class AnyController extends Controller
{
    public function show(): JsonResponse
    {
        return Laravel::get()
            ->add(Error::fromArray(['status' => 500, 'code' => 'first_error']))
            ->add(Error::fromArray(['status' => 500, 'code' => 'second_error']))
            ->add(Error::fromArray(['status' => 500, 'code' => 'third_error']))
            ->json();
    }
}

Response

{
    "errors": [
        {
            "status": 500,
            "code": "first_error"
        },
        {
            "status": 500,
            "code": "second_error"
        },
        {
            "status": 500,
            "code": "third_error"
        }
    ]
}

Laravel Response

You do not need to pass a status code via the json method. The status code will be fetched from the first error you pushed in the bag.

->json()

You can also overwrite the status code with the json method.

->json(null, 401)

Related Packages

intermax/laravel-json-api

Reusable filters, resources and other API tools.

8,303 2
prophets/drupal-jsonapi

ORM for consuming Drupal with the JSON API module.

1,144 1
php-tmdb/laravel

Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the...

53,312 160
kavenegar/laravel

laravel 4 and 5 kavenegar integration

366,462 86
laravel-ja/laravel

The Laravel Framework.

5,667 17