Skip to content

leeduc/json-api-builder

Repository files navigation

Json Api Builder

Latest Version on Packagist Build Status Coverage Status Total Downloads Software License

This package is auto generate data follow jsonapi.org.

Install

Via Composer

$ composer require leeduc/json-api-builder

Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:

'providers' => [
    // ...
    Leeduc\JsonApiBuilder\JsonApiBuilderServiceProvider::class,
]

Next, also in the app.php config file, under the aliases array, you may want to add facades.

'aliases' => [
    // ...
    'JsonApiBuilder' => Leeduc\JsonApiBuilder\Facades\JsonApiBuilder::class,
]

Usage

Build Schema in folder views of resource

posts.view = app\resources\views\posts\show.schema.php

return [
  'id' => $data->id,
  'type' => class_basename($data),
  'attributes' => [
    'name' => $data->name,
    'email' => $data->email
  ],
  'relationships' => [
    'posts' => [
      'partial' => 'posts.show',
      'links' => [
        'self' => route('get_user', ['id' => $data->id]) . '/relationships/posts',
        'related' => route('get_user', ['id' => $data->id]) . '/posts'
      ]
    ],
    'comments' => [
      'partial' => 'comments.show',
      'links' => [
        'self' => route('get_user', ['id' => $data->id]) . '/relationships/comments',
        'related' => route('get_user', ['id' => $data->id]) . '/comments'
      ]
    ]
  ],
  'links' => [
    'self' => route('get_user', ['id' => $data->id])
  ]
];

Build Array

$data = $users = User::with('comments')->paginate(10); // List
$data = $users = User::with('comments')->first(); // Object

$builder = \JsonApiBuilder::setData($data)
                    ->entity('view.path.name', function($data) {
                        $data['id'] = 100;
                        return $data;
                    })
                    ->relationship(['comments'])
                    ->included(['comments' => ['post_id', 'content']]);

dd($builder->parse()); // Array data

Build Json

$builder = \JsonApiBuilder::setData($data)
                    ->entity('package::view.path.name', function($data) {
                        // custom entity data
                        return $data;
                    })
                    ->relationship(['comments'])
                    ->included(['comments'])
                    ->json(['version' => '1.0'])
                    ->meta([
                      'version' => '1.0'
                    ])
                    ->pagination([
                      'next' => 'example/next',
                      'pre' => 'example/pre'
                    ])
                    ->response();

dd($builder); // Class Symfony\Component\HttpFoundation\Response
dd($builder->getContent()); // Get Json

Json response

{
  "data": [
    {
      "id": 1,
      "type": "user",
      "attributes": {
        "name": "Pj2EHmiLOH",
        "email": "Tqxfq6aZDk@gmail.com"
      },
      "links": {
        "self": "http://example.com/user\/1"
      },
      "relationships": {
        "comments": {
          "data": [
            {
              "id": 2,
              "type": "comment"
            },
            {
              "id": 8,
              "type": "comment"
            }
          ],
          "links": {
            "self": "http://example.com/user\/1\/relationships\/comments",
            "related": "http://example.com/user\/1\/comments"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": 2,
      "type": "comment",
      "attributes": {
        "post_id": "3",
        "user_id": "1",
        "content": "UHXLbmJxySxiTTYdjzR539bNXjohgpCVj0WfwvmZWKUonhUipxJeHPh0AtTWqIZpzLZfixawJJEQwqILf93Co5edPOrKDfaqvkSQ"
      },
      "relationships": {
        "user": {
          "data": [
            {
              "id": 1,
              "type": "user"
            }
          ],
          "links": {
            "self": "http://example.com/comment\/2\/relationships\/user"
          }
        }
      }
    },
    {
      "id": 8,
      "type": "comment",
      "attributes": {
        "post_id": "2",
        "user_id": "1",
        "content": "Y8kDX5EOQFtqoy4171bGFVNrvYgMRr9UVHQvD7Eed43YgzeZ1KFJipTFCMJVu6rtb4V8Fm14mv2t3aN26CRNgiOqDsGiMPbQyVJF"
      },
      "relationships": {
        "user": {
          "data": [
            {
              "id": 1,
              "type": "user"
            }
          ],
          "links": {
            "self": "http://example.com/comment\/8\/relationships\/user"
          }
        }
      }
    }
  ],
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "self": "http://example.com/test",
    "first": "http://example.com/test?page%5Bsize%5D=1&page%5Bnumber%5D=1",
    "next": "http://example.com/test?page%5Bsize%5D=1&page%5Bnumber%5D=2",
    "last": "http://example.com/test?page%5Bsize%5D=1&page%5Bnumber%5D=40"
  }
}

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email lee.duc55@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

This package is auto generate data follow jsonapi.org

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages