PHPackages                             shalvah/laravel-jsend - 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. shalvah/laravel-jsend

ActiveLibrary[API Development](/categories/api)

shalvah/laravel-jsend
=====================

Laravel helpers to easily send JSend-compliant responses

2.4(3y ago)62117.4k—5.4%162MITPHPPHP &gt;=7.2.5

Since Jun 21Pushed 3y ago3 watchersCompare

[ Source](https://github.com/shalvah/laravel-jsend)[ Packagist](https://packagist.org/packages/shalvah/laravel-jsend)[ Docs](http://shalvah.me/laravel-jsend)[ RSS](/packages/shalvah-laravel-jsend/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (13)Used By (2)

laravel-jsend
=============

[](#laravel-jsend)

[![Latest Stable Version](https://camo.githubusercontent.com/fdfc724a354778f29dc7f05a4bf03a2a3408798b130499e8c00b82caeb9c3474/68747470733a2f2f706f7365722e707567782e6f72672f7368616c7661682f6c61726176656c2d6a73656e642f762f737461626c65)](https://packagist.org/packages/shalvah/laravel-jsend)[![Total Downloads](https://camo.githubusercontent.com/18ef2dd9f97a0b304992021fc8c9ab4e5a0494151e1258485984577c11f0bc8b/68747470733a2f2f706f7365722e707567782e6f72672f7368616c7661682f6c61726176656c2d6a73656e642f646f776e6c6f616473)](https://packagist.org/packages/shalvah/laravel-jsend)

Simple helpers to generate [JSend-compliant](https://labs.omniti.com/labs/jsend) responses for your Laravel app

The [JSend specification](https://labs.omniti.com/labs/jsend) lays down some rules for how JSON responses from web servers should be formatted. JSend is especially suited for REST-style applications and APIs.

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

[](#installation)

Laravel 7 and above:

```
composer require shalvah/laravel-jsend
```

Laravel 5.1 - 6.\*:

```
composer require shalvah/laravel-jsend:^1.0
```

Usage
-----

[](#usage)

In your controller:

```
public function create(Request $request)
{
  $userData = $request->input('data');
  if (empty($userData['email']))
      return jsend_fail(['email' => 'Email is required']);

  try {
      $user = User::create($userData):
      return jsend_success($user);
  } catch (Exception $e) {
      return jsend_error('Unable to create user: '.$e->getMessage());
  }
}
```

You can also add the `JsendExceptionFormatter` trait in `App\Exceptions\Handler` to format JSON responses for unhandled exceptions and Laravel validation errors as JSend:

```
class Handler extends ExceptionHandler
{
    use Shalvah\LaravelJsend\JsendExceptionFormatter;

    // ...
}
```

Available helpers
-----------------

[](#available-helpers)

### `jsend_success`

[](#jsend_success)

The `jsend_success` function creates a JSend **success** response instance.

```
return jsend_success([
  "id" => 2,
  "title" => "New life",
  "body" => "Trust me, this is great!"
]);
```

Generates a response:

```
{
  "status": "success",
  "data": {
    "id": 2,
    "title": "New life",
    "body": "Trust me, this is great!"
  }
}
```

You may pass an Eloquent model instead of an array as the "data" object:

```
$post = Post::find($id);
return jsend_success($post);
```

### `jsend_fail`

[](#jsend_fail)

The `jsend_fail` function creates a JSend **fail** response instance.

```
return jsend_fail([
    "title" => "title is required",
    "body" => "body must be 50 - 10000 words"
]);
```

Generates a response:

```
{
  "status": "fail",
  "data": {
    "title": "title is required",
    "body": "body must be 50 - 10000 words"
  }
}
```

### `jsend_error`

[](#jsend_error)

The `jsend_error` function creates a JSend **error** response instance.

```
return jsend_error("Unable to connect to database");
```

Generates a response:

```
{
  "status": "error",
  "message":"Unable to connect to database"
}
```

You may also pass optional `code` and `data` objects.

```
return jsend_error("Unable to connect to database", 'E0001', ['type' => 'database error']);
```

Generates a response:

```
{
  "status": "error",
  "message":"Unable to connect to database",
  "code": "E001",
  "data": {
    "type": "database error"
  }
}
```

> Note: for each helper, the HTTP status codes are set automatically (to 200, 400, and 500 for `success`, `fail`, and `error` respectively), and the header `Content-type: application/json` is set. If you wish, you may specify a HTTP status code and additional headers as the last two parameters.

```
return jsend_success($post, 201, ["X-My-Header" => "header value"]);
return jsend_fail(["location_id" => "Location not found"], 404);
return jsend_error("Unable to connect to database", 'E0001', [], 503, ["X-My-Header" => "header value"]);
```

License
-------

[](#license)

MIT

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 72.7% 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 ~192 days

Recently: every ~253 days

Total

12

Last Release

1138d ago

Major Versions

1.4 → 2.02020-06-25

1.5 → 2.12020-09-23

PHP version history (2 changes)1.0PHP &gt;=5.6.4

2.0PHP &gt;=7.2.5

### Community

Maintainers

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

---

Top Contributors

[![shalvah](https://avatars.githubusercontent.com/u/14361073?v=4)](https://github.com/shalvah "shalvah (24 commits)")[![philipnewcomer](https://avatars.githubusercontent.com/u/1446874?v=4)](https://github.com/philipnewcomer "philipnewcomer (3 commits)")[![iraldoad](https://avatars.githubusercontent.com/u/15159678?v=4)](https://github.com/iraldoad "iraldoad (2 commits)")[![marktinsley](https://avatars.githubusercontent.com/u/303598?v=4)](https://github.com/marktinsley "marktinsley (1 commits)")[![flmaasif](https://avatars.githubusercontent.com/u/98513013?v=4)](https://github.com/flmaasif "flmaasif (1 commits)")[![ssx](https://avatars.githubusercontent.com/u/254889?v=4)](https://github.com/ssx "ssx (1 commits)")[![stevegrunwell](https://avatars.githubusercontent.com/u/233836?v=4)](https://github.com/stevegrunwell "stevegrunwell (1 commits)")

---

Tags

apijsendjsonlaraveljsonlaraveljsend

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shalvah-laravel-jsend/health.svg)

```
[![Health](https://phpackages.com/badges/shalvah-laravel-jsend/health.svg)](https://phpackages.com/packages/shalvah-laravel-jsend)
```

###  Alternatives

[aimeos/aimeos-laravel

Cloud native, API first Laravel eCommerce package with integrated AI for ultra-fast online shops, marketplaces and complex B2B projects

8.6k214.7k3](/packages/aimeos-aimeos-laravel)[cloudcreativity/laravel-json-api

JSON API (jsonapi.org) support for Laravel applications.

7881.1M5](/packages/cloudcreativity-laravel-json-api)[joskolenberg/laravel-jory

Create a flexible API for your Laravel application using json based queries.

4513.5k](/packages/joskolenberg-laravel-jory)

PHPackages © 2026

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