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

ActiveLibrary[API Development](/categories/api)

hackerboy/json-api
==================

PHP JSON API implementation (server side)

2.1.8(4y ago)112.0k1MITPHPPHP &gt;=7.1.0CI failing

Since Nov 9Pushed 4y ago1 watchersCompare

[ Source](https://github.com/hackerboydotcom/json-api)[ Packagist](https://packagist.org/packages/hackerboy/json-api)[ RSS](/packages/hackerboy-json-api/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (3)Versions (33)Used By (0)

Current version: 2.x
====================

[](#current-version-2x)

Packagist: hackerboy/json-api
=============================

[](#packagist-hackerboyjson-api)

Making JSON API implementation (server side) easiest for you

Install
-------

[](#install)

```
composer require hackerboy/json-api

```

Run examples:
=============

[](#run-examples)

- Example code: /example/index.php
- Guide to set up /examples/index.php to see some examples of uses.

```
git clone https://github.com/hackerboydotcom/json-api ./hackerboy-json-api;
cd hackerboy-json-api;
composer install --dev;

```

Then config your localhost nginx/apache to access \[LOCALHOST\_PATH\]/examples/index.php

Table of Contents
=================

[](#table-of-contents)

- [How to use? (Making response document)](#how-to-use)

    - [Create your resource schema](#create-your-resource-schema)
    - [Configuration and mapping your resources](#configuration-and-mapping-your-resources)
    - [Document methods](#document-methods)
    - [Implement relationships](#implement-relationships)
    - [Set data as relationships](#set-data-as-relationships)
    - [toArray() and toJson() methods](#toarray-and-tojson-methods)
    - [Easily create elements for your document](#easily-create-elements-for-your-document)
        - [Create errors](#create-errors)
        - [Create links](#create-links)
        - [Create other elements](#create-other-elements)
    - [Document query](#document-query)
        - [Examples of document query](#examples-of-document-query)
- [Client-Side usages](#client-side-usages)

    - [Flexible document and resources](#flexible-document-and-resources)
        - [Example of flexible document](#example-of-flexible-document)
        - [Parse from string or array](#parse-from-string-or-array)

How to use?
===========

[](#how-to-use)

Create your resource schema
---------------------------

[](#create-your-resource-schema)

Mapping your id, type, attributes from your model objects by creating Schema classes. Inside the Schema class, you can retrieve your model object through `$this->model`. For example, I'm gonna talk in a Laravel project context. Firstly, let's create a resource file: /app/Http/JsonApiResources/UserResource.php

```
