Json Api Builder
[
][link-coverrall]
![Total Downloads][ico-downloads]
![Software License][ico-license]
, (*1)
This package is auto generate data follow jsonapi.org., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require leeduc/json-api-builder, (*4)
Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:
``` php
'providers' => [
// ...
Leeduc\JsonApiBuilder\JsonApiBuilderServiceProvider::class,
]
Next, also in the app.php config file, under the aliases array, you may want to add facades., (*5)
``` php
'aliases' => [
// ...
'JsonApiBuilder' => Leeduc\JsonApiBuilder\Facades\JsonApiBuilder::class,
], (*6)
## Usage
Build Schema in folder views of resource
`posts.view` = `app\resources\views\posts\show.schema.php`
``` 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, (*7)
``` php
$data = $users = User::with('comments')->paginate(10); // List
$data = $users = User::with('comments')->first(); // Object, (*8)
$builder = \JsonApiBuilder::setData($data)
->entity('view.path.name', function($data) {
$data['id'] = 100;
return $data;
})
->relationship(['comments'])
->included(['comments' => ['post_id', 'content']]);, (*9)
dd($builder->parse()); // Array data, (*10)
Build Json
``` php
$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
json
{
"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"
}
}, (*11)
Change log
Please see CHANGELOG for more information what has changed recently., (*12)
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*13)
Security
If you discover any security related issues, please email lee.duc55@gmail.com instead of using the issue tracker., (*14)
Credits
License
The MIT License (MIT). Please see License File for more information., (*15)