PHP code example of leeduc / json-api-builder

1. Go to this page and download the library: Download leeduc/json-api-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leeduc / json-api-builder example snippets

 php
'providers' => [
    // ...
    Leeduc\JsonApiBuilder\JsonApiBuilderServiceProvider::class,
]
 php
'aliases' => [
    // ...
    'JsonApiBuilder' => Leeduc\JsonApiBuilder\Facades\JsonApiBuilder::class,
]
 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])
  ]
];

 php
$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'])
                    ->