PHPackages                             delatbabel/jsonresponses - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. delatbabel/jsonresponses

ActiveLibrary[API Development](/categories/api)

delatbabel/jsonresponses
========================

A trait containing common JSON responses for an API built with Laravel 5.1.

v1.2.5(10y ago)0136MITPHPPHP &gt;=5.4.0

Since Dec 9Pushed 9y ago2 watchersCompare

[ Source](https://github.com/delatbabel/jsonresponses)[ Packagist](https://packagist.org/packages/delatbabel/jsonresponses)[ RSS](/packages/delatbabel-jsonresponses/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

JSON Responses
==============

[](#json-responses)

[![Build Status](https://camo.githubusercontent.com/7b6a59b301bc6d086c4a8c44d62188f13c9b34e423bd4f4be2b5cff6013b35a5/68747470733a2f2f7472617669732d63692e6f72672f64656c6174626162656c2f6a736f6e726573706f6e7365732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/delatbabel/jsonresponses)[![StyleCI](https://camo.githubusercontent.com/749b5ad993ed89be4c70e35d6b2e8e0f9a4da85516755f96dfeba18234944758/68747470733a2f2f7374796c6563692e696f2f7265706f732f34373636313739372f736869656c64)](https://styleci.io/repos/47661797)[![Latest Stable Version](https://camo.githubusercontent.com/79e5b21bc82c5ba2c51df5457f8a73039f09d027a22ca4f16ceaa4c888126204/68747470733a2f2f706f7365722e707567782e6f72672f64656c6174626162656c2f6a736f6e726573706f6e7365732f76657273696f6e2e706e67)](https://packagist.org/packages/ddpro/jsonresponses)[![Total Downloads](https://camo.githubusercontent.com/4b7d0360cc6b3a085d82e987542d3a2ec26c6e8d5abc39ce192c27dacde5982a/68747470733a2f2f706f7365722e707567782e6f72672f64656c6174626162656c2f6a736f6e726573706f6e7365732f642f746f74616c2e706e67)](https://packagist.org/packages/delatbabel/jsonresponses)

Common JSON responses for an API built with Laravel 5.1.

Features
--------

[](#features)

This trait does the following:

- It returns generic *(predefined)* JSON responses for common API actions like creating, updating, destroying, indexing and showing a resource.
- It can be easily extended and modified
- It can be used anywhere in your application *(controllers, routes etc...)*

Installation
------------

[](#installation)

```
composer require delatbabel/jsonresponses

```

In your class do this:

```
use Delatbabel\JsonResponses\JsonResponses;

class MyController
{
    use JsonResponses;
    // ...
}
```

Examples
--------

[](#examples)

### Success Response With Data

[](#success-response-with-data)

```
return $this->respondSuccess('OK', ['time' => gmtime()]);
```

### Failure Response

[](#failure-response)

```
return $this->respondInternalError();
```

### Failure Response With Message and Data

[](#failure-response-with-message-and-data)

```
return $this->respondNotAcceptable(
    'Mugwumps are not found in swamps',
    ['location' => 'desert']);
```

Choosing a Status Code
----------------------

[](#choosing-a-status-code)

See  and

Architecture
============

[](#architecture)

Rationale
---------

[](#rationale)

Many APIs have generic response formats for returning a success/fail response, a response code, a message, and some data. There are a few packages out there including Syndra (Mario Basic), which do similar things but I gone back to this, which was my original implementation as a trait, because it does all of what I what I need to do.

This provides generic JSON responses for when a resource is created, updated, destroyed, indexed, etc, as well as a generic error format.

To Trait Or Not To Trait?
-------------------------

[](#to-trait-or-not-to-trait)

Many developers have derided the use of PHP 5.4+ traits on the basis that they are really just methods to hide simple cut/paste code. Certainly they have their disadvantages, not the least of which is difficulty in providing stand-alone tests for them.

In this case I decided to implement this as a trait anyway, because I wanted all of my API controllers to return an identical response format, and this was a simple way of doing it.

A lot of software designers have suggested that traits should not provide data, only methods, which makes them look more exactly like Ruby's "mixins". However PHP does provide limited capability for traits to hold data and so I have implemented a simple protected variable to store the response code between calls.

Response Format
---------------

[](#response-format)

The response format emitted by this trait will look like this (in JSON):

```
response: {
    success: true,      // true or false
    message: message,   // arbitrary message goes here
    response_code: code // response code goes here
},
data: { ...
}                       // arbitrary data structure goes here

```

The HTTP response code is repeated inside the response hash for easy access.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~14 days

Recently: every ~19 days

Total

8

Last Release

3759d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d3bb12da0a07ef99daa73f6e75e84bc3e2e9f90a3523ab428e54deb38643876a?d=identicon)[delatbabel](/maintainers/delatbabel)

---

Top Contributors

[![delatbabel](https://avatars.githubusercontent.com/u/2335362?v=4)](https://github.com/delatbabel "delatbabel (15 commits)")

---

Tags

jsonapilaravellaravel5

### Embed Badge

![Health badge](/badges/delatbabel-jsonresponses/health.svg)

```
[![Health](https://phpackages.com/badges/delatbabel-jsonresponses/health.svg)](https://phpackages.com/packages/delatbabel-jsonresponses)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

815320.5k3](/packages/defstudio-telegraph)[resend/resend-laravel

Resend for Laravel

1212.2M8](/packages/resend-resend-laravel)[essa/api-tool-kit

set of tools to build an api with laravel

53386.5k](/packages/essa-api-tool-kit)[api-platform/laravel

API Platform support for Laravel

59156.3k11](/packages/api-platform-laravel)[simplestats-io/laravel-client

Analytics for Laravel. Track visitors, registrations, and payments. Discover which channels actually drive revenue, not just traffic. Server-side, GDPR compliant, ad-blocker proof.

5019.3k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
