PHPackages                             nilportugues/laravel5-json-api - 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. nilportugues/laravel5-json-api

ActiveLibrary[API Development](/categories/api)

nilportugues/laravel5-json-api
==============================

Laravel 5 JSON API Transformer Package

2.4.7(9y ago)31232.4k70[21 issues](https://github.com/nilportugues/laravel5-jsonapi/issues)[5 PRs](https://github.com/nilportugues/laravel5-jsonapi/pulls)1MITPHP

Since Aug 16Pushed 8y ago18 watchersCompare

[ Source](https://github.com/nilportugues/laravel5-jsonapi)[ Packagist](https://packagist.org/packages/nilportugues/laravel5-json-api)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-laravel5-json-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (55)Used By (1)

Laravel 5 JSON API Server Package
=================================

[](#laravel-5-json-api-server-package)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6f4003ac1901f9030fa3d5b3b9ac0c16a17bca3d4c29fda9db545c12fe5b6df2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f6c61726176656c352d6a736f6e6170692d7472616e73666f726d65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/laravel5-jsonapi-transformer/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/5fb992d183aa029e1ad86947959978bfe28ac70945cac0714a264c9ee5364008/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32326462383866352d643036312d346233322d626164312d3462383036616330373331382f6d696e692e706e67)](https://insight.sensiolabs.com/projects/22db88f5-d061-4b32-bad1-4b806ac07318)[![Latest Stable Version](https://camo.githubusercontent.com/4b67bd61b93183c8f0598f34560451d2f58a662fd23af4295541274e53df386c/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d6a736f6e2d6170692f762f737461626c65)](https://packagist.org/packages/nilportugues/laravel5-json-api)[![Total Downloads](https://camo.githubusercontent.com/a29ffca1b73cc9b8d2385b0c97f197f57429eb37010796b1c4c91dcd9d8be37d/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d6a736f6e2d6170692f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/laravel5-json-api)[![License](https://camo.githubusercontent.com/3aedb608e6fd73042bbd643a8380313a977ce61706d27d2704c51c4ca0557b23/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d6a736f6e2d6170692f6c6963656e7365)](https://packagist.org/packages/nilportugues/laravel5-json-api)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

*Compatible with Laravel 5.0, 5.1 &amp; 5.2*

- Package provides a full implementation of the **[JSON API](https://github.com/json-api/json-api)** specification, and is **featured** on the official site!
- A **JSON API Transformer** that will allow you to convert any mapped object into a valid JSON API resource.
- Controller boilerplate to write a fully compiliant **JSON API Server** using your **exisiting Eloquent Models**.
- Works for Laravel 5 and Lumen frameworks.

---

- [Installation](#installation)
- [Configuration (Laravel 5 &amp; Lumen)](#configuration-laravel-5--lumen)
    - [Configuration for Laravel 5](#configuration-for-laravel-5)
        - [Step 1: Add the Service Provider](#step-1-add-the-service-provider)
        - [Step 2: Defining routes](#step-2-defining-routes)
        - [Step 3: Definition](#step-3-definition)
        - [Step 4: Usage](#step-4-usage)
    - [Configuration for Lumen](#configuration-for-lumen)
        - [Step 1: Add the Service Provider](#step-1-add-the-service-provider-1)
        - [Step 2: Defining routes](#step-2-defining-routes-1)
        - [Step 3: Definition](#step-3-definition-1)
        - [Step 4: Usage](#step-4-usage-1)
- [JsonApiController](#jsonapicontroller)
- [Examples: Consuming the API](#examples-consuming-the-api)
    - [GET](#get)
    - [POST](#post)
    - [PUT](#put)
    - [PATCH](#patch)
    - [DELETE](#delete)
- [GET Query Params: include, fields, sort and page](#get-query-params-include-fields-sort-and-page)
- [POST/PUT/PATCH with Relationships](#postputpatch-with-relationships)
- [Custom Response Headers](#custom-response-headers)
- [Common Errors and Solutions](#common-errors-and-solutions)

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

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
composer require nilportugues/laravel5-json-api

```

Now run the following artisan command:

```
php artisan vendor:publish

```

Configuration (Laravel 5 &amp; Lumen)
-------------------------------------

[](#configuration-laravel-5--lumen)

For the sake of having a real life example, this configuration will guide you on how to set up **7 end-points** for two resources, `Employees` and `Orders`.

Both `Employees` and `Orders` resources will be **Eloquent** models, being related one with the other.

Furthermore, `Employees`will be using an Eloquent feature, `appended fields` to demonstrate how it is possible to make the most of Eloquent and this package all together.

### Configuration for Laravel 5

[](#configuration-for-laravel-5)

#### Step 1: Add the Service Provider

[](#step-1-add-the-service-provider)

Open up `config/app.php` and add the following line under `providers` array:

```
'providers' => [
    //...
    NilPortugues\Laravel5\JsonApi\Laravel5JsonApiServiceProvider::class,
],
```

#### Step 2: Defining routes

[](#step-2-defining-routes)

We will be planning the resources ahead its implementation. All routes require to have a name.

This is how our `app/Http/routes.php` will look:

```
